pogibas
pogibas

Reputation: 28329

Merge columns cut & cat

I have file.txt 3 columns.

1 A B
2 C D
3 E F

I want to add #1&#3 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

Answers (3)

dj_segfault
dj_segfault

Reputation: 12409

cat file.txt | awk '{print $1 $2 "\n" $1 $3};'

Upvotes: 1

glenn jackman
glenn jackman

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:

  • I assume *tmp was a typo
  • I assume as this point the directory only contains "tmp1" and "tmp2"

Look 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

pzanoni
pzanoni

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

Related Questions