concatenate lines in pairs

Hi,

I have a text file with the following contents

/C=IT/O=INFN/OU=Personal Certificate/L=Napoli/CN=Some guy
/C=IT/O=INFN/CN=INFN CA
/O=Grid/O=NorduGrid/OU=uninett.no/CN=Another guy
/O=Grid/O=NorduGrid/CN=NorduGrid Certification Authority
/C=TW/O=AP/OU=GRID/CN=Someone else
/C=TW/O=AS/CN=Academia Sinica Grid Computing Certification Authority Mercury

I need concatenate the lines of this file in pairs, thus it should become

/C=IT/O=INFN/OU=Personal Certificate/L=Napoli/CN=Some guy /C=IT/O=INFN/CN=INFN CA
/O=Grid/O=NorduGrid/OU=uninett.no/CN=Another guy  /O=Grid/O=NorduGrid/CN=NorduGrid Certification Authority
/C=TW/O=AP/OU=GRID/CN=Someone else  /C=TW/O=AS/CN=Academia Sinica Grid Computing Certification Authority  Mercury

Similar to the example in UNIX BASH scripting: Concatenate lines using awk in bash

How can I do that?

Thnx

# cat infile
/C=IT/O=INFN/OU=Personal Certificate/L=Napoli/CN=Some guy
/C=IT/O=INFN/CN=INFN CA
/O=Grid/O=NorduGrid/OU=uninett.no/CN=Another guy
/O=Grid/O=NorduGrid/CN=NorduGrid Certification Authority
/C=TW/O=AP/OU=GRID/CN=Someone else
/C=TW/O=AS/CN=Academia Sinica Grid Computing Certification Authority Mercury
# sed '/C=IT/N;s/\n/ / ;/O=Grid/N;s/\n/ / ;/C=TW/N;s/\n/ / ' infile
/C=IT/O=INFN/OU=Personal Certificate/L=Napoli/CN=Some guy /C=IT/O=INFN/CN=INFN CA
/O=Grid/O=NorduGrid/OU=uninett.no/CN=Another guy /O=Grid/O=NorduGrid/CN=NorduGrid Certification Authority
/C=TW/O=AP/OU=GRID/CN=Someone else /C=TW/O=AS/CN=Academia Sinica Grid Computing Certification Authority Mercury
1 Like

Hi
Try this;

   sed 'N;s/\n/ /' file

Guru.

1 Like

Thank you,

my file has more than 500 of these pairs with different combinations, so I need something more generic...

What I tried was to use awk, unset the RS, match Start of line/anytext\nStart of line/anytext\n, use \n as field separator on the matched pattern, print $1 and $2, and then move to text pair, but my attemps failed...

---------- Post updated at 09:58 AM ---------- Previous update was at 09:56 AM ----------

That worked, thnx a lot!

Hi.

The paste command can do this:

#!/usr/bin/env bash

# @(#) s1	Demonstrate joining pairs of lines from a single file, paste.

pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

pl " Results from paste:"
cat data1 | paste -d "" - -

exit 0

producing:

% ./s1

-----
 Results from paste:
/C=IT/O=INFN/OU=Personal Certificate/L=Napoli/CN=Some guy/C=IT/O=INFN/CN=INFN CA
/O=Grid/O=NorduGrid/OU=uninett.no/CN=Another guy/O=Grid/O=NorduGrid/CN=NorduGrid Certification Authority
/C=TW/O=AP/OU=GRID/CN=Someone else/C=TW/O=AS/CN=Academia Sinica Grid Computing Certification Authority Mercury

cheers, drl

Here is awk solution:

awk 'NR%2{ORS=" ";print;next}{ORS="\n";print}' file