Random Line Deletion in C Files

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will learn how to use the sed command to replace lines in a C file randomly. We will write a script that will randomly delete 20% of the lines from the file and replace them with a string <-- DEL -->. We will also learn how to create random numbers and edit files using the sed command.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/wc("`Text Counting`") linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/printf("`Text Formatting`") linux/TextProcessingGroup -.-> linux/sed("`Stream Editing`") linux/BasicSystemCommandsGroup -.-> linux/bc("`Arithmetic Calculations`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/VariableHandlingGroup -.-> shell/str_manipulation("`String Manipulation`") shell/VariableHandlingGroup -.-> shell/arrays("`Arrays`") shell/VariableHandlingGroup -.-> shell/param_expansion("`Parameter Expansion`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_ops("`Arithmetic Operations`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills shell/if_else -.-> lab-18872{{"`Random Line Deletion in C Files`"}} linux/cat -.-> lab-18872{{"`Random Line Deletion in C Files`"}} linux/wc -.-> lab-18872{{"`Random Line Deletion in C Files`"}} linux/exit -.-> lab-18872{{"`Random Line Deletion in C Files`"}} linux/echo -.-> lab-18872{{"`Random Line Deletion in C Files`"}} linux/pipeline -.-> lab-18872{{"`Random Line Deletion in C Files`"}} linux/redirect -.-> lab-18872{{"`Random Line Deletion in C Files`"}} linux/printf -.-> lab-18872{{"`Random Line Deletion in C Files`"}} linux/sed -.-> lab-18872{{"`Random Line Deletion in C Files`"}} linux/bc -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/shebang -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/comments -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/quoting -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/variables_decl -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/variables_usage -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/str_manipulation -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/arrays -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/param_expansion -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/for_loops -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/cond_expr -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/exit_status -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/arith_ops -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/arith_expansion -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/cmd_substitution -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/subshells -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/adv_redirection -.-> lab-18872{{"`Random Line Deletion in C Files`"}} shell/globbing_expansion -.-> lab-18872{{"`Random Line Deletion in C Files`"}} linux/wildcard -.-> lab-18872{{"`Random Line Deletion in C Files`"}} end

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:

  1. Provide a .c file to the script through the command-line.
  2. Randomly delete 20% of the lines from the file.
  3. 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”);
        #...
}

Summary

After completing this challenge, we have learned how to use the sed command to replace lines in a C file randomly. We have also learned how to create random numbers and edit files using the sed command. This knowledge can be useful in automating tasks that involve editing files. We can use this script to replace lines in any C file and customize it according to our needs.

Other Linux Tutorials you may like