concatenate lines in pairs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting concatenate lines in pairs
# 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:
# 3  
Old 06-17-2010
Hi
Try this;
Code:
   sed 'N;s/\n/ /' file


Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Concatenate files by pairs

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... (2 Replies)
Discussion started by: jawad
2 Replies

2. Shell Programming and Scripting

Remove lines with duplicate pairs where AB is equal to BA

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... (2 Replies)
Discussion started by: sammy777
2 Replies

3. UNIX for Dummies Questions & Answers

Concatenate three lines into one

Hi. I'm new to this forum. I am attempting to parse an Audit Log from Cognos/TM1, selecting only Event IDs of "client" which are found on the "start-tag" record. These Logs are in a pseudo-XML format but not a true XML format. I want to FTP an Audit Log File from the Cognos server to our UNIX... (7 Replies)
Discussion started by: FredAtArrow
7 Replies

4. Shell Programming and Scripting

File w/ many line pairs--how do I order based on 1st lines only?

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... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

5. UNIX for Advanced & Expert Users

Parse (delimited string) key-value pairs in a column into separate lines

Hi experts, e.g. i/p data looks like 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified|| o/p data should like - row1: 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747|unidentified ... (1 Reply)
Discussion started by: sumoka
1 Replies

6. Shell Programming and Scripting

How to swap order of pairs of lines?

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. (8 Replies)
Discussion started by: rd5817
8 Replies

7. Shell Programming and Scripting

Concatenate lines between lines starting with a specific pattern

Hi, I have a file such as: --- >contig00001 length=35524 numreads=2944 gACGCCGCGCGCCGCGGCCAGGGCTGGCCCA CAGGCCGCGCGGCGTCGGCTGGCTGAG >contig00002 length=4242 numreads=43423 ATGCCGAAGGTCCGCCTGGGGCTGG CGCCGGGAGCATGTAGCG --- I would like to concatenate the lines not starting with ">"... (9 Replies)
Discussion started by: s052866
9 Replies

8. Shell Programming and Scripting

Concatenate two lines in one

Does anyone know a way I can fix my file below where it has lines that are cut? Just like the (notice the space before ms,c=PH).. dn: cn=english,ou=Messaging,ou=lang,o=subject,t=j ms,c=PH which should be dn: cn=english,ou=Messaging,ou=lang,o=subject,t=jms,c=PH The whole text file... (5 Replies)
Discussion started by: Orbix
5 Replies

9. UNIX Desktop Questions & Answers

How to concatenate consecutive lines

I have a few lines like -- feature 1, subfeat 0, type 3, subtype 1, value 0, -- feature 1, subfeat 0, type 1, subtype 1, value 0, I would like to concatenate the... (1 Reply)
Discussion started by: shivi707
1 Replies

10. Shell Programming and Scripting

reading lines in pairs from file in ksh

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? (4 Replies)
Discussion started by: ytokar
4 Replies
Login or Register to Ask a Question