Linux command read 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.

Читайте также:  Linux редакторы в консоли

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.

#!/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!

Читайте также:  Kali linux network nat

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 line by line from standard input Bash [duplicate]

read will read a line of input into name name1 name2 . splitting the line based on the contents of the Internal Field Separator ( IFS ). The default for IFS is ' \t\n' (that is space tab newline ). If you only provide a single variable to read , you will read the entire line into that variable (unless you have set a new delimiter with the -d option to read ). If you provide more than one variable, (e.g. read -r name name1 ) word splitting will occur based on the current value of IFS . Meaning if you provide the string hello world to:

name="hello world" . On the other hand, if you provide the same string to:

name="hello" , name1="world" . What if you have excess words in the line but only 2 variables? Say your string is now "hello big wide world" , what happens with:

name="hello" , name1="big wide world" . The words in string are assigned to your variables in order and if there are insufficient variables to hold each word in the string, the last variable will contain all remaining words in the string not previously assigned.

You change how word splitting occurs by altering IFS . Take a close look at the answer provided by anubhava for an example. You are free to specify any character you would like the words to be split on. (helpful in say parsing a csv file to set IFS=$',\n' and have the words split on ',' instead of space)

To ensure you read an entire line into a variable, you can provide only a single variable to read and set IFS='$\n' to ensure word splitting only occurs on newline . (Note: providing the change as part of the while loop limits the IFS alteration to the scope of that loop. For example:

while IFS=$'\n' read -r line; do # do whatever with line done 

Will ensure that each line on stdin will be read into line while preserving normal word-splitting outside the loop. Inside the loop you can then add each line to an array as anubhava shows in his answer. (to preserve all whitespace IFS= is used)

Источник

Read File Line by Line in Bash

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

Читайте также:  Docker server ostype contains linux

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.

Источник

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