Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Not sure on how to turn this input:

A
B
C
apartner
bpartner
cpartner

into this output:

A apartner
B bpartner
c cpartner

I have this pattern in a 80 line file that needs to be 40 line. Also, how can I enclose each item, in lets say, <il><b>A</b> <i>apartner</i></il>?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

Using pr from coreutils:

pr -2 -t -l 40 -s' ' $oldfile > $newfile

Edit (after your edit), using the same pr and sed:

pr -2 -t -l 40 -J -S'</b> <i>' $oldfile | sed 's|^|<il><b>|g;s|$|</i></il>|g' > $newfile
share|improve this answer
add comment

[GNU]

If it's not important to use sed or awk then I recommend this as the easiest solution:

First you split the file in two:

head -n 40 file >file1
tail -n 40 file >file2

Then you combine the lines:

paste --delimiters=" " file1 file2

Instead of head and tail you can use sed or awk, of course:

sed -n -e 1,40p combinedfile >file1
sed -n -e 41,80p combinedfile >file2
awk 'NR < 41 {print;}' combinedfile >file1
awk 'NR > 40 {print;}' combinedfile >file2

OK, I just realize: The pure awk solution isn't as complicated as I thought:

awk 'NR < 41 { lines[NR]=$0; next; }; {print lines[NR-40],$0}' combinedfile
share|improve this answer
1  
you can skip creating file1 and file2: paste <(head -40 file) <(tail -40 file ) –  mmckinst Jun 12 '13 at 23:06
    
Simpler and much understandable. Thanks. –  mirandalol Jun 20 '13 at 12:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.