Abhishake Yadav Abhishake Yadav - 6 months ago 13
Linux Question

Separate two columns from one file to two separate files in for loop

I have a file which looks like :

00000981.ffid 00000982.ffid
00000982.ffid 00000983.ffid
00000983.ffid 00000984.ffid
00000984.ffid 00000985.ffid
00000985.ffid 00000986.ffid
00000986.ffid 00000987.ffid
00000987.ffid 00000988.ffid
00000988.ffid 00000989.ffid
00000989.ffid 00000990.ffid
00000990.ffid 00000991.ffid
00000991.ffid 00000992.ffid
00000992.ffid 00000993.ffid
00000993.ffid 00000994.ffid
00000994.ffid 00000995.ffid
00000995.ffid 00000996.ffid
00000996.ffid 00000997.ffid
00000997.ffid 00000998.ffid
00000998.ffid 00000999.ffid
00000999.ffid 00001000.ffid
00001000.ffid 00001001.ffid
00001001.ffid 00001002.ffid
00001002.ffid 00001003.ffid
00001003.ffid 00001004.ffid
00001004.ffid 00001005.ffid
00001005.ffid 00001006.ffid
00001006.ffid 00001007.ffid
00001007.ffid


I want to separate the two columns into two separate variables and use them in a loop for generating further output from these files.

Assuming name of the file is list_c

I am using this code :

for filename in $(cat /d/home/adira0151/Desktop/tmp/list_c)

do

echo "$filename" | awk '{print $1}' >> /d/home/adira0151/Desktop/tmp/C_ffid

echo "$filename" | awk '{print $2}' >> /d/home/adira0151/Desktop/tmp/N_ffid

done


But what I get is C_ffid :

00000981.ffid
00000982.ffid
00000982.ffid
00000983.ffid
00000983.ffid
00000984.ffid
00000984.ffid
00000985.ffid
00000985.ffid
00000986.ffid
00000986.ffid
00000987.ffid
00000987.ffid
00000988.ffid
00000988.ffid
00000989.ffid
00000989.ffid
00000990.ffid
00000990.ffid
00000991.ffid
00000991.ffid
00000992.ffid
00000992.ffid
00000993.ffid
00000993.ffid
00000994.ffid
00000994.ffid
00000995.ffid
00000995.ffid
00000996.ffid
00000996.ffid
00000997.ffid
00000997.ffid
00000998.ffid
00000998.ffid
00000999.ffid
00000999.ffid
00001000.ffid
00001000.ffid
00001001.ffid
00001001.ffid
00001002.ffid
00001002.ffid
00001003.ffid
00001003.ffid
00001004.ffid
00001004.ffid
00001005.ffid
00001005.ffid
00001006.ffid
00001006.ffid
00001007.ffid
00001007.ffid


Whereas the other file is blank.

Please help.

Answer

To write column 1 and column 2 to separate files:

awk '{print $1>"/path/to/C_ffid"; print $2>"/path/to/N_ffid"}' list_c

How it works

  • print $1>"/path/to/C_ffid"

    This command writes the first column to file /path/to/C_ffid.

  • print $2>"/path/to/N_ffid"

    This command writes the second column to file /path/to/N_ffid.

Sample output

For your input file, the following two output files are created:

$ cat C_ffid 
00000981.ffid
00000982.ffid
00000983.ffid
00000984.ffid
00000985.ffid
00000986.ffid
00000987.ffid
00000988.ffid
00000989.ffid
00000990.ffid
00000991.ffid
00000992.ffid
00000993.ffid
00000994.ffid
00000995.ffid
00000996.ffid
00000997.ffid
00000998.ffid
00000999.ffid
00001000.ffid
00001001.ffid
00001002.ffid
00001003.ffid
00001004.ffid
00001005.ffid
00001006.ffid
00001007.ffid

And:

$ cat N_ffid 
00000982.ffid
00000983.ffid
00000984.ffid
00000985.ffid
00000986.ffid
00000987.ffid
00000988.ffid
00000989.ffid
00000990.ffid
00000991.ffid
00000992.ffid
00000993.ffid
00000994.ffid
00000995.ffid
00000996.ffid
00000997.ffid
00000998.ffid
00000999.ffid
00001000.ffid
00001001.ffid
00001002.ffid
00001003.ffid
00001004.ffid
00001005.ffid
00001006.ffid
00001007.ffid

Looping and processing in shell

while read one two
do
   echo "$one">>C_ffid
   echo "$two">>N_ffid
   echo "Processing $one and $two"
done <list_c