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.

  • Care to share which bits of text comprise which columns? – tink Jul 29 '14 at 21:09
  • column1 = chr1:16840617-16840780 ;column2 = RNU1-1 – user3138373 Jul 29 '14 at 21:11
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.

  • 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: unix.stackexchange.com/questions/24260/… – 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 <in >out – mikeserv Jul 29 '14 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.