Bash linux read file line by line

Read line by line in Bash script

The best way to do this is to redirect the file into the loop:

# Basic idea. Keep reading for improvements. FILE=test while read CMD; do echo "$CMD" done < "$FILE" 

There are some additional improvements that could be made:

  • Add IFS= so that read won't trim leading and trailing whitespace from each line.
  • Add -r to read to prevent backslashes from being interpreted as escape sequences.
  • Lower-case CMD and FILE . The Bash convention is that only environmental and internal shell variables are uppercase.
  • Use printf in place of echo which is safer if $cmd is a string like -n , which echo would interpret as a flag.
file=test while IFS= read -r cmd; do printf '%s\n' "$cmd" done < "$file" 

use

What you have is piping the text "cat test" into the loop.

cat test | \ while read CMD; do echo $CMD done 

xargs is the most flexible solution for splitting output into command arguments.

It is also very human readable and easy to use due to its simple parameterisation.

Format is xargs -n $NUMLINES mycommand .

For example, to echo each individual line in a file /tmp/tmp.txt you'd do:

cat /tmp/tmp.txt | xargs -n 1 echo 

Or to diff each successive pair of files listed as lines in a file of the above name you'd do:

cat /tmp/tmp.txt | xargs -n 2 diff 

The -n 2 instructs xargs to consume and pass as separate arguments two lines of what you've piped into it at a time.

You can tailor xargs to split on delimiters besides carriage return/newline.

Use man xargs and google to find out more about the power of this versatile utility.

Источник

How to read file line by line in Bash script

How would you write a Bash script that can process a text file one line at a time. First you need a syntax and approach to read the file line by line. The methods for this approach are shown in this tutorial.

Suppose, you have a file named company.txt which contents the company names. This file contains the following content.

Example -1: Reading file content from command line

Suppose, you want to read the file, company.txt, line by line from the command line without ‘cat’ command. Run the following command to do the task. while loop will read each line from the file company.txt in each step and store the content of the line in $line variable which will be printed later.

Example -2: Reading file content using script

Create a bash file and add the following code to read the content of a particular file. Here, an existing filename is stored in $filename variable and $n variable is used to keep the value of the line number of that file. Like previous example, while loop is used to read this file with line number.

Читайте также:  Linux mounting usb device

#!/bin/bash
filename = 'company.txt'
n = 1
while read line; do
# reading each line
echo "Line No. $n : $line "
n =$ ( ( n+ 1 ) )
done < $filename

Run the following command to execute the script.

Run ‘cat’ command with company.txt file to display the original content of company.txt file.

Example -3: Passing filename from the command line and reading the file

Create a bash file and add the following script. This script will take the filename from the command line argument. First argument value is read by the variable $1 which will contain the filename for reading. If the file exists in the current location then while loop will read the file line by line like previous example and print the file content.

Run the above script with employee.txt file as argument value. The output will show the content of employee.txt file by removing extra space. You can show the original content of employee.txt file by using ‘cat’ command.

Example – 4: Reading file by omitting backslash escape

If you want to read each line of a file by omitting backslash escape then you have to use ‘-r’ option with read command in while loop.

Create a file named company2.txt with backslash and run the following command to execute the script. The output will show the file content without any backslash.

You will need to read the file for many programming purposes. For example, you can search or match any particular content easily from any file by reading each line separately. So, it is an essential task for any programming. Some simple examples of reading file in bash script are shown in this tutorial. These will help you to get the idea of reading file content line by line using while loop in bash script and apply in your script more efficiently. For more information watch the video!

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

Read File Line by Line in Bash

Here are a couple of ways for reading file line by line in the Bash shell.

You may find yourself in a situation where you want to use a shell script to read files line by line.

And in this tutorial, I will be covering multiple ways to read file line by line in the bash script.

Reading a file line by line in bash

In this tutorial, I will walk you through two ways to write a bash script by which you can read file line by line:

To make things easy to understand, I will be using a simple text file named LHB.txt which contains the following:

1. Ubuntu 2. Arch 3. openSUSE 4. Fedora 5. Slackware

1. Using read command with while loop

As the method name suggests, here, I will be using the read command with the while loop (inside the script of course).

First, create and open a simple script file using the following command:

And paste the following lines (will explain in a moment):

#!/bin/bash file="LHB.txt" while read -r line; do echo -e "$line\n" done  
  • file="LHB.txt" : Shows which file I want to work with and in my case, it's LHB.txt.
  • while read -r line; do : Starts while loop and reads lines one by one until there's no line to read and the -r flag will prevent backslash escaping within the lines.
  • echo -e "$line\n" : Will print every line and each will be separated by one blank line.
  • done < "$file" : It redirects the input from the specified file to the while loop.

Make the script executable using the chmod command as shown:

And finally, execute the script:

read file line by line in bash

2. Using the cat command with the while loop

In this method, I will be using the cat command to read the file which will make the loop logic simple to understand.

This is the script I will be using:

#!/bin/bash cat LHB.txt | while IFS= read -r line; do echo "$line" echo # Print a blank line done 
  • cat LHB.txt | : Reads the file content of the LHB.txt file and passes it to piped another argument.
  • while IFS= read -r line; do : It reads lines one by one of the LHB.txt files and. IFS is used to preserve leading and trailing whitespace.
  • echo "$line" : Prints the line stored in the line variable.

Once you execute the above command, you can expect the following results:

use cat command to read line by line in bash

Learn bash for free

If you are just getting started or want to brush up on the basics, we have a dedicated series for you:

I hope you will find this guide helpful.

Источник

How to Read Files Line by Line in Bash

Reading a file line by line allows you to effectively process a file's contents and output each line as an element in a list. After displaying each line separately, search for or match any specific content easily.

One of the ways to read a text file in individual lines is to use the Bash shell.

In this tutorial, you will learn to read a file line by line in Bash.

Learn five different methods for reading a file line by line in Bash.

  • A system running Linux.
  • Access to a terminal (Ctrl + Alt + T).
  • A text editor (such as Nano or vi/vim).

Reading Line by Line in Bash

There are several methods for reading a file line by line using Bash. The following sections highlight five methods for processing a file one line at a time using Bash.

Method 1: Using Read Command and While Loop

The first method is to use the read command and a while loop in a Bash script. While it is possible to do the same in a terminal, Bash scripts save the code and make it reusable. Follow the steps below:

1. Open the terminal (Ctrl + Alt + T) and create a new Bash script using a text editor such as vi/vim:

2. Enter the following lines:

#!/bin/bash file="days.txt" while read -r line; do echo -e "$line\n" done 
  • The $file variable is defined after the shebang line (the first line in Bash scripts), and it stores the path to the input file you want to process.
  • The -r argument appended to the read command prevents the interpretation of any backslash-escaped characters while reading the file's contents.

Note: Prevent the read command from trimming leading/trailing whitespaces by setting the internal field separator to an empty string - IFS= .

  • Each line's contents are stored in the $line variable. Within the while loop, the echo command prints the $line variable's contents. The -e argument allows echo to interpret special characters such as the newline character \n .
  • The while loop continues until it reaches the end of the file and the loop ends.

Note: Use printf instead of echo to print and format the output. Learn to use the Bash printf command.

Reading a file line by line using the read command and the while loop.

The script outputs each line of the example text file separately.

Method 2: Using cat Command and for Loop

Another method to display a file's contents in individual lines is to use the cat command and the for loop. The for loop allows echo to print the lines from the cat command output until it reaches the end of the file.

Note: Learn more about the Bash for loop and see examples of using the loop.

2. Enter the following lines:

#!/bin/bash file=$(cat days.txt) for line in $file do echo -e "$line\n" done
  • The $file variable stores the input file's contents using the cat command.
  • The for loop iterates through each line of the cat command output and prints it using the echo command until it reaches the end of the file.

Reading a file line by line using the cat command and for loop.

The script outputs the file's contents line by line in standard output.

Method 3: Using here Strings

Another method of printing a file's contents line by line is to use a here string to feed the file's contents to the read command. The here string connects the contents of a variable, string, or file specified after the syntax to the standard input of the invoked program.

Note: A here string is a simpler form of a here document.

1. Create a new Bash script:

2. Enter the following lines:

#!/bin/bash while IFS= read -r line; do printf '%s\n' "$line" done 
  • In the while loop, the IFS= argument is an empty string to prevent trimming whitespaces.
  • The -r argument prevents the interpretation of backslash-escaped characters.
  • The printf command prints each line of the file. The format specifiers treat the input as a string ( %s ) and add a newline character ( \n ) after each line.
  • The here string feeds the cat command's output to the read command.

3. Save the script and exit the editor:

Reading a file line by line using the here strings.

The output prints the file's contents line by line.

Method 4: Using File Descriptors

A file descriptor refers to an open file or process. Each process has three default file descriptors:

Provide the input for the read command using a file descriptor and output each line from the file's contents separately. Follow the steps below:

1. Create a new bash script:

2. Enter the following lines:

#!/bin/bash while IFS= read -r -u9 line; do printf '%s\n' "$line" done 9< days.txt
  • In the while loop, instruct the read command to read input from a file descriptor by specifying the -u argument and the file descriptor number.

Important: When specifying file descriptors, use a number between 4 and 9 to avoid conflict with the internal shell file descriptors.

  • The printf command treats the input $line variable as a string ( %s ) and adds a newline character ( \n ) after printing the $line contents.
  • The 9 syntax contains the same file descriptor number as in the while loop. The input file's contents are sent to the specified file descriptor.

4. Execute the script to test the code:

Reading a file line by line using file descriptors.

The script output prints each line of the file separately.

Method 5: Using Process Substitution

Process substitution allows the standard output of a process (or processes) to appear as a file, and feeds it into another process' standard input. Use process substitution to supply the input file and print each file line separately.

2. Enter the following lines:

#!/bin/bash while IFS= read -r line; do printf '%s\n' "$line" done < <(cat days.txt)

Источник

Оцените статью
Adblock
detector