Joining lines in reverse. append line 1 to line 2.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining lines in reverse. append line 1 to line 2.
# 1  
Old 07-31-2008
Joining lines in reverse. append line 1 to line 2.

Hi I have used many times the various methods to append two lines together in a file.

This time I want to append the 1st line to the second and repeat for the complete file.... an example

This is the file

owns the big brown dog
joe
owns the small black dog
jim

What I want is

joe owns the big brown dog
jim owns the small black dog

Thanks. Dave
# 2  
Old 07-31-2008
Code:
#  awk '{a=$0;getline;print  $0,a}' file
joe owns the big brown dog
jim owns the small black dog

# 3  
Old 07-31-2008
Thanks for your help. That is exactly what I wanted.

Can you explain what happens please.

Thanks Tytalus
# 4  
Old 07-31-2008
Using sed
Code:
$ sed -n 'N; s/\(^.*\)\n\(.*$\)/\2 \1/p' file
joe owns the big brown dog
jim owns the small black dog
$

# 5  
Old 07-31-2008
Just for fun, pure shell solution:
Code:
i=0
while read line
    do if [ $(($i%2)) == 1 ]; then
        echo "$line $lastline"
    else
        lastline=$line
    fi
    let i=$i+1
done

# 6  
Old 07-31-2008
Or:

Code:
perl -00 -pe's/(.*\n)(.*)\n/$2 $1/g' filename

Or:

Code:
while IFS= read -r y;IFS= read -r x;do 
  printf "%s\n" "$x $y"
done<filename


Last edited by radoulov; 08-04-2008 at 08:29 AM..
# 7  
Old 08-04-2008
Code:
perl -ne 'if($.%2==1){$t=$_;} if($.%2==0){$_=~tr/\n//d;print $_," ";print $t; $t="";}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Append next line to previous lines when NF is less than 0

Hi All, This is very urgent, I've a data file with 1.7 millions rows in the file and the delimiter is cedilla and I need to format the data in such a way that if the NF in the next row is less than 1, it will append that value to previous line. Any help will be appricated. Thanks,... (17 Replies)
Discussion started by: cumeh1624
17 Replies

3. Shell Programming and Scripting

joining multi-line file into single lines

Hi, I have a file like mentioned below..For each specific id starting with > I want to join the sequence in multiple lines to a single line..Is there a simple way in awk or sed to do this >ENST00000558922 cdna:KNOWN TCCAGGATCCAGCCTCCCGATCACCGCGCTAGTCCTCGCCCTGCCTGGGCTTCCCCAGAG... (2 Replies)
Discussion started by: Diya123
2 Replies

4. UNIX for Dummies Questions & Answers

append following lines to 1st line, every 3 lines

I have output like this: USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 ... where USER_ID is a unique user login followed by their login timestamp and... (6 Replies)
Discussion started by: MaindotC
6 Replies

5. Shell Programming and Scripting

Joining multi-line output to a single line in a group

Hi, My Oracle query is returing below o/p ---------------------------------------------------------- Ins trnas value a lkp1 x a lkp1 y b lkp1 a b lkp2 x b lkp2 y ... (7 Replies)
Discussion started by: gvk25
7 Replies

6. Shell Programming and Scripting

how to append multiple lines to the last line of a file

Hello, This is what I am trying to achieve: file1 a b c d file2 e f g h (8 Replies)
Discussion started by: smarones
8 Replies

7. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

8. Shell Programming and Scripting

Append text to end of line on all lines

Hi, I've spent some time researching for this but can't seem to find a solution. I have a file like this 1234|Test|20101111|18:00|19:00There will be multiple lines in the file with the same kind of format. For every line I need to make it this 1234|Test|20101111|18:00|19:00||create... (5 Replies)
Discussion started by: giles.cardew
5 Replies

9. Shell Programming and Scripting

Joining contents in multiple lines to a single line

I do have a file with contents splited into multiple lines ADSLHLJASHGLJSKAGHJJGAJSLGAHLSGHSAKBV AJHALHALHGLAGLHGBJVFBJVLFDHADAH GFJAGJAGAJFGAKGAKGFAK AJHFAGAKAGAGKAKAKGKAGFGJDGDJJDGJDJDFAG ... ... .... 100's of lines I would like to rearrange the content of this file so it will be a... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

10. UNIX for Dummies Questions & Answers

Joining lines to single line in VI

Dear friends, In VI, I have these data shown below: Line1 Line2 Line3 Line4 How can I JOIN these line to the first line? When I finished I should have: Line1 Line2 Line3 Line4 is there a text length limit of how long a single line can be in VI? Thank you much! (10 Replies)
Discussion started by: bobo
10 Replies
Login or Register to Ask a Question