Reputation: 107
An input file list_c, 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 two output files.
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 N_ffid is blank.
Please help.
Upvotes: 1
Views: 83
Reputation: 8446
Using cut, assuming *list_c_ is tab separated:
cd /d/home/adira0151/Desktop/tmp/
cut -f 1 < list_c > C_ffid
cut -f 2 < list_c > N_ffid
If space separated, this would work:
tr -s ' ' '\t' < list_c | cut -f 1 > C_ffid
tr -s ' ' '\t' < list_c | cut -f 2 > N_ffid
Note, running over one input file twice is inefficient, these would benefit from using a parallel input util like pee, e.g.:
tr -s ' ' '\t' < list_c | pee "cut -f 1 > C_ffid" "cut -f 2 > N_ffid"
Or, if tab separated:
pee "cut -f 1 > C_ffid" "cut -f 2 > N_ffid" < list_c
Upvotes: 0
Reputation: 8446
POSIX shell script, conceptually similar to the OP's code, sans awk:
cd /d/home/adira0151/Desktop/tmp/
while read a b ; do
echo "$a" >> C_ffid
echo "$b" >> N_ffid
done < list_c
Upvotes: 0
Reputation:
The problem in your code is, You are using the awk to print the column1 and column2. The awk command works with space as a separator for the column separating. In your file have the more than one space in between the two columns. So, only you get the second file as blank. If you want it in awk, change your file, with single space between the two columns and execute. It also will work.
Otherwise,
You can use the cut command to delimit that and store it into the variable In the starting and you can use those variables in the loop.
Assume in between both columns have the tab. The below code will do what you expect. The var1 variable contains the first column values and var2 variable contains the seconds column values.
var1=`cut -d' ' -f1 list_c`
var2=`cut -d' ' -f2 list_c`
for filename in $var1
do
echo "$filename" >>C_ffid
done
for filename in $var2
do
echo "$filename" >>C_ffid1
done
But In any way make sure about the delimiter for separating in cut command or awk command.
Upvotes: 1
Reputation: 113994
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
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
.
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
while read one two
do
echo "$one">>C_ffid
echo "$two">>N_ffid
echo "Processing $one and $two"
done <list_c
Upvotes: 1