Reputation: 28329
I have file.txt 3 columns.
1 A B
2 C D
3 E F
I want to add #1 as the end of #2. Result should look like this:
1A
2C
3E
1B
2D
3F
I am doing this by
cut -f 1,2 > tmp1
cut -f 1,3 > tmp2
cat *tmp * > final_file
But I am getting repeated lines! If I check the final output with:
cat * | sort | uniq -d
there are plenty of repeated lines and there are none in the primary file.
Can anyone suggest other way of doing this? I believe the one I am trying to use is too complex and that's why I am getting such a weird output.
Upvotes: 1
Views: 1559
Reputation: 246764
Preserves the order with one pass through the file
awk '
{print $1 $2; pass2 = pass2 sep $1 $3; sep = "\n"}
END {print pass2}
' file.txt
The reason this (cat tmp* * > final_file
) is wrong:
*tmp
was a typoLook at how those wildcards will be expanded:
tmp*
expands to "tmp1" and "tmp2"*
also expands to "tmp1" and "tmp2"So your command line becomes cat tmp1 tmp2 tmp1 tmp2 > final_file
and hence you get all the duplicated lines.
Upvotes: 1
Reputation: 3039
pzanoni@vicky:/tmp$ cat file.txt
1 A B
2 C D
3 E F
pzanoni@vicky:/tmp$ cut -d' ' -f1,2 file.txt > result
pzanoni@vicky:/tmp$ cut -d' ' -f1,3 file.txt >> result
pzanoni@vicky:/tmp$ cat result
1 A
2 C
3 E
1 B
2 D
3 F
I'm using bash
Upvotes: 3