1

I have a text file like this

chr1:16840617-16840780    RNU1-1             (2 columns are tab separated)
chr3:142139047-142139211    RNU1-100P
............
............

I want to loop over the lines of text file and save column 1 and column 2 in sep variables, something like this :

OLDIFS=$IFS; IFS=$'\n'; for line in $(cat test.txt);do LOC="save location";NAME="save name";done

After saving name and location I have to do couple of steps more to get my desired output but as of now I want to store them.

Nidal
  • 8,856
  • 11
  • 55
  • 74
user3138373
  • 2,441
  • 6
  • 29
  • 44

1 Answers1

2

Try

while read column1 column2
do
  something with $column1
  something more $column2
  ... 
done < test.txt

Using the read directly from a while & redirected file saves you the cat (completely unnecessary use of ;}) and changing IFS.

tink
  • 6,160
  • 2
  • 21
  • 30
  • Since I was using for loop I think I need to change the IFS because if I am reading a file line by line it doesn't take into account spaces – user3138373 Jul 29 '14 at 21:16
  • The spaces are the reason why while loops are usually the preferred option. Look at the accepted answer here: http://unix.stackexchange.com/questions/24260/reading-lines-from-a-file-with-bash-for-vs-while – tink Jul 29 '14 at 21:21
  • You can stream out in the same you stream in the same you stream in like `while...process...echo "$RESULT"...done out` – mikeserv Jul 29 '14 at 21:26