Make or use delimiter to separate results

54 views Asked by At

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
3

There are 3 answers

0
fedorqui On BEST ANSWER

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.

4
nu11p01n73R On

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
0
anubhava On

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]+'