Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading file line by line (with space) in Unix Shell scripting - Issue

Tags:

shell

unix

ksh

I want to read a file line by line in Unix shell scripting. Line can contain leading and trailing spaces and i want to read those spaces also in the line. I tried with "while read line" but read command is removing space characters from line :( Example if line in file are:-

abcd efghijk
 abcdefg hijk

line should be read as:- 1) "abcd efghijk" 2) " abcdefg hijk"

What I tried is this (which not worked):-

while read line
do
   echo $line
done < file.txt

I want line including space and tab characters in it. Please suggest a way.

like image 897
Sourabh Saxena Avatar asked May 28 '13 10:05

Sourabh Saxena


People also ask

How do you read a file line by line in Unix shell script?

Syntax: Read file line by line on a Bash Unix & Linux shell file. 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.

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.

Does space matter in shell script?

The lack of spaces is actually how the shell distinguishes an assignment from a regular command. Also, spaces are required around the operators in a [ command: [ "$timer"=0 ] is a valid test command, but it doesn't do what you expect because it doesn't recognize = as an operator.


2 Answers

Try this,

IFS=''
while read line
do
    echo $line
done < file.txt

EDIT:

From man bash

IFS - The Internal Field Separator that is used for word
splitting after expansion and to split lines into words
with  the  read  builtin  command. The default value is
``<space><tab><newline>''
like image 125
sat Avatar answered Oct 18 '22 18:10

sat


You want to read raw lines to avoid problems with backslashes in the input (use -r):

while read -r line; do
   printf "<%s>\n" "$line"
done < file.txt

This will keep whitespace within the line, but removes leading and trailing whitespace. To keep those as well, set the IFS empty, as in

while IFS= read -r line; do
   printf "%s\n" "$line"
done < file.txt

This now is an equivalent of cat < file.txt as long as file.txt ends with a newline.

Note that you must double quote "$line" in order to keep word splitting from splitting the line into separate words--thus losing multiple whitespace sequences.

like image 23
Jens Avatar answered Oct 18 '22 17:10

Jens