-1

I have SQL output with multiple results:

 1 OI 021 141012 1321 0001242 4S 2 0080004 5 001 00014 6 000001 000000000000016973 6 0529540437 0000000000
2963526113
test 7 8 000000001 9
 1 OI 021 141012 1321 0001242 4S 2 0080004 5 001 00014 6 000001 000000000000016973 6 0529540437 0000000000
2963526113
test 7 8 000000001 9

The question is how to make delimiter or something else and to join every second and third row to the first and turn every SQL query to one row them in that output:

1 OI 021 141012 1321 0001242 4S 2 0080004 5 001 00014 6 000001 000000000000016973 6 0529540437 00000000002963526113 test 7 8 000000001 9
1 OI 021 141012 1321 0001242 4S 2 0080004 5 001 00014 6 000001 000000000000016973 6 0529540437 00000000002963526113 test 7 8 000000001 9
mpapec
  • 50,217
  • 8
  • 67
  • 127
Kalin Borisov
  • 1,091
  • 1
  • 12
  • 23

3 Answers3

2

Awk love these !!

$ awk 'NR%3 == 0{print line;line=""; next} {line = line $0}' test
1 OI 021 141012 1321 0001242 4S 2 0080004 5 001 00014 6 000001 000000000000016973 6 0529540437 00000000002963526113
1 OI 021 141012 1321 0001242 4S 2 0080004 5 001 00014 6 000001 000000000000016973 6 0529540437 00000000002963526113

What it does??

  • NR%3 == 0 Checks if the number of records, NR is a multiple of 3 if yes prints the entire content of line variable. Resets the line line variable and next causes it to read the next record

  • {line = line $0} will be excecuted for NR is not a multiple of 3, copies the entire line $0 onto line variable

OR

much simpler

$ awk '!(NR%3){print line;line=""; next} {line = line $0}' test
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
1

You can use paste with as many - as lines you want to join:

paste - - - < file

or

... | paste - - -

It returns:

I 021 141012 1321 0001242 4S 2 0080004 5 001 00014 6 000001 000000000000016973 6 0529540437 0000000000  2963526113  test 7 8 000000001 9
1 OI 021 141012 1321 0001242 4S 2 0080004 5 001 00014 6 000001 000000000000016973 6 0529540437 0000000000   2963526113  test 7 8 000000001 9

If you need to use another delimiter instead of space, use -d: for example, paste -d"|" - - - <file.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
1

Using gnu-awk and custom RS:

awk -v RS='\n[0-9]+ [A-Z]+ [0-9]+' '{gsub(/\n/, "")} NR==1{print; p=RT; next} 
        {gsub(/\n/, "", p); print p $0; p=RT}' file
1 OI 021 141012 1321 0001242 4S 2 0080004 5 001 00014 6 000001 000000000000016973 6 0529540437 00000000002963526113test 7 8 000000001 9
1 OI 021 141012 1321 0001242 4S 2 0080004 5 001 00014 6 000001 000000000000016973 6 0529540437 00000000002963526113test 7 8 000000001 9

PS: This will work for records that are broken out it in any number of lines as long as starting fields have this regex pattern: [0-9]+ [A-Z]+ [0-9]+'

anubhava
  • 761,203
  • 64
  • 569
  • 643