join line 1-2,3-4 etc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting join line 1-2,3-4 etc
# 1  
Old 04-11-2011
join line 1-2,3-4 etc

hello all

i have text file like this

aaa
bbb
ccc
ddd
eee
fff


i hope result like
aaa bbb
ccc ddd
eee fff

so line 1-2 is merge, 3-4 merge

if using sed/awk how do this ?

thanks
# 2  
Old 04-11-2011
Using paste command.
Code:
paste - -< inputfile

OR with space as delimiter.
Code:
paste -d" " - -< inputfile

# 3  
Old 04-11-2011
Code:
sed 'N;s/\n/ /' infile

Code:
awk '{getline nxt ; print $0 " " nxt}' infile


Last edited by Chubler_XL; 04-11-2011 at 02:58 AM..
# 4  
Old 04-11-2011
Code:
 
xargs -n2 < inputfile

# 5  
Old 04-11-2011
Quote:
Originally Posted by Chubler_XL
Code:
sed 'N;s/\n/ /' infile

If the solution needs to print the last line of a file with an odd number of lines, for most sed implementations, that will fail to print the last line. It's not my intention to disparage this solution, but I'd just like to point out that it depends on a gnu sed deviation from posix. posix states that the N command must not print the pattern space if there is not another line of input; gnu sed developers deliberately decided to break all sed scripts that use the N command when they implemented an N command whose default behavior is to always print the pattern space.

For most sed implementations (and gnu sed with the POSIXLY_CORRECT environment variable set to a non-null value), the sed script would be:
Code:
sed '$!N; s/\n/ /'

A version that should be (cannot test since I don't have a gnu sed available at this moment) portable between gnu sed's default behavior and a posixly-correct-behaving implementation:
Code:
sed '${p; d;}; N; s/\n/ /'

It's disappointing (to say the least) that gnu chose to complicate matters when there was so little to be gained.

Regards,
Alister

Last edited by alister; 04-11-2011 at 11:30 AM..
# 6  
Old 04-11-2011
thank all, allready solve
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join the line on delimiter using sed/awk in UNIX

I've input as , abcd| ef 123456| 78| 90 Desired output as, abcdef 1234567890 Anyone please give the solution. (5 Replies)
Discussion started by: jinixvimal
5 Replies

2. Shell Programming and Scripting

Join two lines into one, but the second line only the last two columns

Hi guys, I hope you are doing well! I have a file and I need to join two lines into one, but the second line I need only the last two columns. ================= "eHealth Trend Report","logoRpt" "LAN/WAN Group 123" "Divide by Time" "switch1_a-RH-Serial0" "BW: 1.02 M" ... (4 Replies)
Discussion started by: antoniorajr
4 Replies

3. Shell Programming and Scripting

Join common patterns in multiple lines into one line

Hi I have a file like 1 2 1 2 3 1 5 6 11 12 10 2 7 5 17 12 I would like to have an output as 1 2 3 5 6 10 7 11 12 17 any help would be highly appreciated Thanks (4 Replies)
Discussion started by: Harrisham
4 Replies

4. Shell Programming and Scripting

How to join output of two command in same line?

Hi I am trying to figureout how to join output of two command in unix in the same line. for e.g. $ankit : df -h | egrep -w /home | awk '{print -n $1 "\t" $5}' ;stat --format=%a /home Output: 0148G /home 775 I want this output as... 0148G /home 775 Please let me know if you... (7 Replies)
Discussion started by: kotak86
7 Replies

5. Shell Programming and Scripting

Search a text, and Join the next line.

Hello Everybody, I am New to Unix Environment, and need some help. Basically i have got a file named XYZ.txt with following pattern. CR Len:102 Typ:1 Nro:71818037 Chk:1 Tim:2011-07-16/08:46:03 oiw:3 oTy:145 oAd:50206 diw:0 dTy:145 dAd:919399951328 SMC:919821900040 iTm:2011-07-16/08:46:03... (5 Replies)
Discussion started by: spiabhi
5 Replies

6. Shell Programming and Scripting

How join line and add | in front

Hello, Did any one know how to use perl join line and add | in front Input--> timestamp=2009-11-10-04.55.20.829347; a; b; c; ddaa; timestamp=2009-11-10-04.55.20.829347; aa; bb; cc; Output--> ... (2 Replies)
Discussion started by: happyday
2 Replies

7. Shell Programming and Scripting

How to use Perl to join multi-line into single line

Hello, Did anyone know how to write a perl script to merge the multi-line into a single line where each line with start at timestamp Input--> timestamp=2009-11-10-04.55.20.829347; a; b; c; timestamp=2009-11-10-04.55.20.829347; aa; bb; cc; (5 Replies)
Discussion started by: happyday
5 Replies

8. Shell Programming and Scripting

join lines on line break in files

i had a file where lines appear to be broken when they shouldn't eg Line 1. kerl abc sdskd sdsjkdlsd sdsdksd \ Line 2. ksdkks sdnjs djsdjsd i can do a shift join to combine the lines but i there are plenty of files with this issue Line 1. kerl abc sdskd sdsjkdlsd sdsdksd ksdkks sdnjs... (6 Replies)
Discussion started by: mad_man12
6 Replies

9. Shell Programming and Scripting

Join same line from multiple file

Hi, Can we use shell script to combine multiple file ? For example we have 3 file 1.txt 2.txt 3.txt and the result should be : (6 Replies)
Discussion started by: justbow
6 Replies

10. Shell Programming and Scripting

Shell script to join a line to the line that follows it

Hi All, I need a shell script to join a line to the line that follows it. But I shouldn't do it for all the lines. I need to join a line having the character say '&' at the end of the line & need to join the line that follows it. E.g Input 1. This is the first line & 2. and the second... (6 Replies)
Discussion started by: shashi_kiran_v
6 Replies
Login or Register to Ask a Question