concatenate lines in pairs


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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 10:50 AM.. Reason: Please use code tags
# 2  
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 11:05 AM..
This User Gave Thanks to ygemici For This Post:
# 4  
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  
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  
Here is awk solution:
Code:
awk 'NR%2{ORS=" ";print;next}{ORS="\n";print}' file

Login or Register to Reply

|
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
Hi, I would like to batch concatenate files by pairs. I have quite a few of them so I would not like to do that pair by pair separately. the names of the file is of the type: file1.fastq newfile1_new.fastq file2.fastq newfile2_new.fastq and so on... I would like to concatenate file1...... UNIX for Dummies Questions & Answers
2
UNIX for Dummies Questions & Answers
Remove lines with duplicate pairs where AB is equal to BA
sammy777
I have a file with four columns like dmn10003t1 PF00001 PF00022 dmn12390t1 dmn10008t1 PF00069 PF00027 dmn9781t1 dmn10008t1 PF00068 PF00027 dmn9781t1 dmn10008t1 PF00069 PF00069 dmn9781t1 dmn12390t1 PF00069 PF00076 dmn10003t1 I want to create a new file by comparing the repeated word pairs...... Shell Programming and Scripting
2
Shell Programming and Scripting
File w/ many line pairs--how do I order based on 1st lines only?
pathunkathunk
I have a file in which the data is stored in pairs of lines. The first line (beginining with ">") is a header, the second line is a sequence. I would like to sort the file by species name. Desired output for the example file: I can use sort -t'_' -k2 to alphabetize headers in the...... Shell Programming and Scripting
1
Shell Programming and Scripting
How to swap order of pairs of lines?
rd5817
This seems to be a question whose answer uses sed or awk. For a file like: a b c d e How to swap the order of the line pairs, to end up with: b a d c e All lines from the original file need to wind up in the output file.... Shell Programming and Scripting
8
Shell Programming and Scripting
reading lines in pairs from file in ksh
ytokar
I need to read pairs of lines from a file and compare them. We can assume that the number of lines in the file is even. Can i do it in korn shell?... Shell Programming and Scripting
4
Shell Programming and Scripting