Icarus
Icarus

Reputation: 1463

Shell read redundant new line character

I have a text file:

A1   A2
B1   B2

I use the script to read the values one by one

cat $TXT | while read FILE
do
DATA1=`(echo $FILE | cut -d' ' -f1)`
DATA2=`(echo $FILE | cut -d' ' -f2)`
done

However DATA2 will read the extra \n character example: DATA2 = A2\n.

How can I read the data without getting the extra character?

Thanks very much.

Upvotes: 2

Views: 108

Answers (1)

John Zwinck
John Zwinck

Reputation: 249123

while read DATA1 DATA2 REST
do
    # DATA1 and DATA2 are already set now
done < "$TXT"

This has the added advantage of spawning several fewer processes per line of input. Namely, my way spawns zero processes, whereas yours spawns something like 4N + 1.

Upvotes: 3

Related Questions