Print File Lines Using Bash Commands

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will write a shell script to print the contents of a file from a given line number to the next given number of lines. We will use the head and tail commands to achieve this. The script will take three command-line arguments: the starting line number, the number of lines to print, and the filename.


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`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/FunctionsandScopeGroup(["`Functions and Scope`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/head("`File Beginning Display`") linux/BasicFileOperationsGroup -.-> linux/tail("`File End Display`") 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`") 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/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/FunctionsandScopeGroup -.-> shell/func_def("`Function Definition`") 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`") subgraph Lab Skills shell/if_else -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} linux/cat -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} linux/head -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} linux/tail -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} linux/exit -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} linux/echo -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} linux/pipeline -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} linux/redirect -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/shebang -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/comments -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/quoting -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/variables_decl -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/variables_usage -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/cond_expr -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/exit_status -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/func_def -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/arith_expansion -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/cmd_substitution -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/subshells -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/adv_redirection -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} shell/globbing_expansion -.-> lab-18301{{"`Print File Lines Using Bash Commands`"}} end

Print Lines

In this challenge, you will create a script to print the contents of a file from a given line number for a specified number of lines.

Tasks

You need to complete the following task:

  • Create a script that prints n lines from the given starting line.

Requirements

You must meet the following requirements:

  • The script should be named printLines.sh.
  • Working directory: ~/project.
  • Accept three command-line arguments:
    • 1- starting line number
    • 2- number of lines
    • 3- filename

Example

labex:project/ $ bash printLines.sh 3 5 inputFile.txt
name
and
i
like
to
labex:project/ $ bash printLines.sh 10 2 inputFile.txt
arch
linux

Summary

After completing this challenge, you will have learned how to use file filter commands in shell scripting. Specifically, you will have learned how to use the head and tail commands to print a specific range of lines from a file. This knowledge can be applied to various scenarios where you need to extract specific information from a file.

Other Linux Tutorials you may like