Linux read lines of file

Attempt2

You don’t need to maintain an index with your while loop. You can append to an array like this: myarray+=($line) . If you need to increment an integer, you can do (( index++ )) or (( index += 1 )) .

@DennisWilliamson ((index++)) has a return value, which will likely terminate the script if run in set -e mode. The same applies to let index++ . Using A=$((A+1)) is safe.

@DennisWilliamson I like it, because it is efficient and because of that very useful. set -eu is my standard prelude.

6 Answers 6

The readarray command (also spelled mapfile ) was introduced in bash 4.0.

When I made that comment, I may not have been sure if it was in 4.0, or 4.1, or 4.2. Anyway, the bash release notes confirm it was added in 4.0.

readarray doesn’t use IFS ; it only populates the named array with one line per element, with no field splitting.

I would suggest adding -t to the answer to strip off the newline characters. Makes it easier to use the array (e.g. for string comparisons) and it is not often that you’ll want to keep the newline anyway.

@AquariusPower bash 4.4 will add a -d flag to readarray to specify an alternate character to terminate each line of the input.

Latest revision based on comment from BinaryZebra’s comment and tested here. The addition of command eval allows for the expression to be kept in the present execution environment while the expressions before are only held for the duration of the eval.

Use $IFS that has no spaces\tabs, just newlines/CR

$ IFS=$'\r\n' GLOBIGNORE='*' command eval 'XYZ=($(cat /etc/passwd))' $ echo "$" sync:x:5:0:sync:/sbin:/bin/sync 

Also note that you may be setting the array just fine but reading it wrong — be sure to use both double-quotes «» and braces <> as in the example above

Please note the many warnings about my answer in comments about possible glob expansion, specifically gniourf-gniourf’s comments about my prior attempts to work around

With all those warnings in mind I’m still leaving this answer here (yes, bash 4 has been out for many years but I recall that some macs only 2/3 years old have pre-4 as default shell)

Other notes:

Can also follow drizzt’s suggestion below and replace a forked subshell+cat with

The other option I sometimes use is just set IFS into XIFS, then restore after. See also Sorpigal’s answer which does not need to bother with this

Читайте также:  Компоненты ядра ос linux могут быть

Источник

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)

Источник

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.

Источник

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