-5

I want to change a text file that contains for example :

a 1
b 2
a 3
b 4

to:

a b
1 2
3 4

any idea how I can accomplish that? I am not familiar with awk but I think it is the way to go?

  • 2
    There are lots of ways this could be done in `awk`, `python`, `perl`, etc. Someone here *might* write it for you if they need reputation, but in general you're expected to take a stab at the problem yourself before asking for help. – Tom Zych Nov 09 '15 at 09:57
  • thanks tom for clarifying it didn't know that kind of question isn't right here. I'll read awk guide then hopefully will get the answer. thank you – Peter Hayek Nov 09 '15 at 11:22
  • just noticed my post is totally fine and millions of people asking those kind of questions on stackoverflow, here is an example: http://stackoverflow.com/questions/5429840/eliminate-duplicate-lines-and-keep-the-last-one. – Peter Hayek Nov 19 '15 at 15:35
  • Hi Peter, yes, that was asked back in 2011. It seems like people have gotten more impatient since then, in general. I posted about that issue on meta but it didn't get a whole lot of discussion. – Tom Zych Nov 19 '15 at 15:41
  • hi, ok here is a 2015 one : http://stackoverflow.com/questions/33756062/remove-empty-lines-from-a-text-file-shell-script/33756089#33756089 – Peter Hayek Nov 19 '15 at 15:50

1 Answers1

1

I'm assuming the input is always two columns, the first column contains the column headers of the output repeated over and over, and that the output may contain one or more columns.

$ cat t.awk
{ sep = (FNR % n == 0) ? "\n" : " " }
NR==FNR { printf $1 sep; if (sep == "\n") nextfile; next; }
{ printf $2 sep }

The number of output columns is set with -v when invoking awk. Note also that the input file needs to be provided twice on the command line as the script does a quick initial pass to print out the output column headers before starting over to print the data (too lazy to deal with arrays).

Two-column output:

$ cat file.txt
a 1
b 2
a 3
b 4
a 5
b 6

$ awk -v n=2 -f t.awk file.txt file.txt
a b
1 2
3 4
5 6

Three-column output:

$ cat file2.txt
a 1
b 2
c 3
a X
b Y
c Z
a @
b #
c $

$ awk -v n=3 -f t.awk file2.txt file2.txt
a b c
1 2 3
X Y Z
@ # $
jas
  • 10,715
  • 2
  • 30
  • 41