concatenate lines in pairs

Tags
shell scripts

 
Thread Tools Search this Thread
# 1  
Old 06-17-2010
concatenate lines in pairs

Hi,

I have a text file with the following contents

Code:
/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

Code:
/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

Last edited by Scott; 06-17-2010 at 11:50 AM.. Reason: Please use code tags
# 2  
Old 06-17-2010
Code:
# 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

Code:
# 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


Last edited by ygemici; 06-17-2010 at 12:05 PM..
This User Gave Thanks to ygemici For This Post:
kerl (06-17-2010)
# 3  
Old 06-17-2010
Hi
Try this;
Code:
   sed 'N;s/\n/ /' file


Guru.
This User Gave Thanks to guruprasadpr For This Post:
kerl (06-17-2010)
# 4  
Old 06-17-2010
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 ----------

Quote:
Originally Posted by guruprasadpr
Hi
Try this;
Code:
   sed 'N;s/\n/ /' file

Guru.
That worked, thnx a lot!
# 5  
Old 06-17-2010
Hi.

The paste command can do this:
Code:
#!/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:
Code:
% ./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
# 6  
Old 06-17-2010
Here is awk solution:
Code:
awk 'NR%2{ORS=" ";print;next}{ORS="\n";print}' file


|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Concatenate files by pairs jawad UNIX for Dummies Questions & Answers 2 06-16-2015 04:39 AM
Remove lines with duplicate pairs where AB is equal to BA sammy777 Shell Programming and Scripting 2 10-27-2014 04:11 PM
Concatenate three lines into one FredAtArrow UNIX for Dummies Questions & Answers 7 08-14-2014 05:03 PM
File w/ many line pairs--how do I order based on 1st lines only? pathunkathunk Shell Programming and Scripting 1 08-03-2013 04:45 PM
How to concatenate lines with specific pattern? nixtime Shell Programming and Scripting 8 06-25-2013 06:39 PM
Parse (delimited string) key-value pairs in a column into separate lines sumoka UNIX for Advanced & Expert Users 1 11-25-2012 07:08 PM
Need to concatenate spuriously separated lines lemele Shell Programming and Scripting 16 12-28-2011 02:21 AM
Concatenate two lines in a fIle uday.sena.m Programming 3 09-22-2011 12:50 AM
How to swap order of pairs of lines? rd5817 Shell Programming and Scripting 8 01-17-2011 11:12 AM
Concatenate lines between lines starting with a specific pattern s052866 Shell Programming and Scripting 9 10-24-2010 01:09 PM
Concatenate multiple lines based. indrajit_u Shell Programming and Scripting 8 06-30-2010 06:50 PM
Concatenate two lines in one Orbix Shell Programming and Scripting 5 04-29-2010 05:40 PM
Concatenate lines in a single paragraph cgkmal Shell Programming and Scripting 4 05-17-2009 02:02 AM
How to concatenate consecutive lines shivi707 UNIX Desktop Questions & Answers 1 01-12-2009 09:08 AM
reading lines in pairs from file in ksh ytokar Shell Programming and Scripting 4 02-08-2008 02:50 PM