Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read line by line from a variable in shell scripting

Tags:

bash

shell

I have a script variable which is multi-line. How do i traverse this variable to read it line by line and process each line the way I want?

like image 870
sasidhar Avatar asked Aug 10 '12 12:08

sasidhar


People also ask

How can I read line by line from a variable in bash?

Syntax: Read file line by line on a Bash Unix & Linux shell The -r option passed to read command prevents backslash escapes from being interpreted. Add IFS= option before read command to prevent leading/trailing whitespace from being trimmed. while IFS= read -r line; do COMMAND_on $line; done < input. file.

How do I read a text file line by line in shell?

We can use the read command to read the contents of a file line by line. We use the -r argument to the read command to avoid any backslash-escaped characters. In the following example, we can see that we have an iteration over a file line by line and we store the contents of a single line in the variable “line“.

How read file line by line in shell script and store each line in a variable?

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.

How do you print a line by line in bash?

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.


2 Answers

Consider the following multi-line variable

x=$(echo -e "a\nb\nc d e")

and a simple process for each line: just echo it with a prefix=LINE: and with single quotes around the line. Either of the following codes will satisfy that requirement:

while read line; do echo "LINE: '${line}'"; done <<< "$x"

or

while read line; do echo "LINE: '${line}'"; done < <(echo "$x")

Neither creates a subshell (so you can, e.g., set variables in the loop and access them outside of it), and both output

LINE: 'a'
LINE: 'b'
LINE: 'c d e'

But suppose instead you have

x=$(echo -e "a \n b\nc d e")
# note--------^--^

and that leading and trailing whitespace matter for your application (e.g., parsing Git porcelain). Both the above codes will give exactly the same output for the latter variable/data as for the former, which is not what you want. To preserve leading and trailing whitespace, replace while read line with while IFS= read -r line . I.e., either of the following codes

while IFS= read -r line; do echo "LINE: '${line}'"; done <<< "$x"

or

while IFS= read -r line; do echo "LINE: '${line}'"; done < <(echo "$x")

will produce

LINE: 'a '
LINE: ' b'
LINE: 'c d e'

See Greg Wooledge's excellent Bash FAQ for details.

like image 99
Karoly Horvath Avatar answered Oct 19 '22 07:10

Karoly Horvath


Although I typically use "while read" for processing multi-line variables, I recently had an instance where it removed the leading space from each line in a file. Using this instead fixed my issue:

printf '%s\n' "$var" | while IFS= read -r line
do
   echo "$line"
done

Code taken from this Unix Stack Exchange answer.

Edit: updated to fix last line issue as suggested by Nicolae Iotu

like image 20
Seth McCauley Avatar answered Oct 19 '22 09:10

Seth McCauley