File Line Replacement Challenge
In this challenge, you are tasked with creating a script to replace 20% of the lines in a C file randomly with a specific pattern.
Tasks
You need to complete the following task:
- Provide a
.c
file to the script through the command-line.
- Randomly delete 20% of the lines from the file.
- Wherever you delete a line, replace it with the string
<-- DEL -->
.
Requirements
You must meet the following requirements:
- The script should be named
replaceLines.sh
.
- Working directory should be
~/project
.
- Knowledge about the
sed
command.
- Understanding how to generate random numbers.
- Editing files using the
sed
command.
Example
labex:project/ $ bash replaceLines.sh main.c
[OUTPUT] Number of Lines in the given file main.c : 12 [OUTPUT]
[OUPUT] Number of Lines that need to be replaced: 2 [OUTPUT]
[OUTPUT] lines that are replaced : 4 9 [OUTPUT]
labex:project/ $ cat main.c
#incude <stdio.h>
int main()
{
<---------------- DEL -------------------->
printf(“Hello world1\n”);
printf(“Hello world2\n”);
printf(“Hello world3\n”);
printf(“Hello world4\n”);
<---------------- DEL -------------------->
printf(“Hello world6\n”);
#...
}