Help Moving lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Moving lines
# 1  
Old 12-27-2011
Help Moving lines

Hi,

I tried going through the existing threads, but couldn't find a solution to my problem. My input file is:

Code:
199.15.2.7|gxi.wxxx.com/|(Mozilla/4.0)|
ASCII text|950604ad05
70.38.9.7|mmg10.com/2011/1.gif|(Mozilla/4.0)|
application/x-ms-dos-executable|a2f332Y02d0
70.38.9.2|vim105.com/113856471.gif|(Mozilla/4.0)|
application/x-ms-dos-executable|aed7d0cK80a0

My output file should look like:
Code:
199.15.2.7|gxi.wxxx.com/|(Mozilla/4.0)|ASCII text|950604ad05
70.38.9.7|mmg10.com/2011/1.gif|(Mozilla/4.0)|application/x-ms-dos-executable|a2f332Y02d0
70.38.9.2|vim105.com/113856471.gif|(Mozilla/4.0)|application/x-ms-dos-executable|aed7d0cK80a0

How do I do this in sed or awk? It should look for the | symbol at end of line, and if it is present, bring the line below that up.

Any help with explanation would be appreciated
# 2  
Old 12-27-2011
Try:
Code:
awk 'ORS=NR%2 ? " " : "\n"' file

# 3  
Old 12-27-2011
Code:
perl -pe '/\|$/&&s/\n$//' inputfile

/\|$/ --> Searches for | symbol at the end of line
&& --> logical 'and' operator.
s/\n$// --> Replace \n at the end with null
This User Gave Thanks to balajesuri For This Post:
# 4  
Old 12-27-2011
Thank you both.

Both solutions worked, but am going with balajesuri's code because I understood his explanation Smilie
# 5  
Old 12-27-2011
Quote:
Originally Posted by balajesuri
Code:
perl -pe '/\|$/&&s/\n$//' inputfile

/\|$/ --> Searches for | symbol at the end of line
&& --> logical 'and' operator.
s/\n$// --> Replace \n at the end with null
You can bypass the logical and by using:
Code:
 perl -pe 's/\|\s*\n/\|/' inputfile

# 6  
Old 12-27-2011
Another one...
Code:
awk '/^[0-9]/{printf $0;next}1' infile

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

2. Shell Programming and Scripting

awk two succeeding lines and moving matching files

Hello everyone I have a few hundreds of .mol2 files that has this pattern @<TRIPOS>ATOM 2 H18 65.2220 Du 1 RES1 0.0000 @<TRIPOS>BOND 1 3 5 ar @<TRIPOS>SUBSTRUCTURE among them, some of the files are missing the line after the @<TRIPOS>BOND and they look... (2 Replies)
Discussion started by: Error404
2 Replies

3. UNIX for Dummies Questions & Answers

Moving lines of file to command line

I have a file in which each line is the name of another file. Is there a way to serve them to the command line? For example, if the file contains file1.txt file2.txt file3.txt ... file9.txt is there a way to insert them in the command as a batch? If I ran a command like grep... (4 Replies)
Discussion started by: wbport
4 Replies

4. Shell Programming and Scripting

not moving !!

Hi I have written the following piece of code to move all files with name *.sh to *.bak the o/p i am getting is *.sh.bak Pls help cat filename | while read line do mv $line $line.sh done filename file conatins file.sh file1.sh file2.sh expected o/p is file.bak (2 Replies)
Discussion started by: ultimatix
2 Replies

5. Shell Programming and Scripting

deleting particular lines and moving a line up using perl/sed

Hi, I need convert a dump file in the following format : (please note that line numbers are provided for easy look) Original file: 1 2007-10-2482.90 No trade 0 0.00 100000.00 2 100000.00 3 0.00 4 HOLD 5 2007-10-2589.75 Bought 1114 1114 100000.00 0.00 ... (5 Replies)
Discussion started by: sabyasm
5 Replies

6. Shell Programming and Scripting

Moving lines within a txt file

A newbie to shell scripting..... I need some assistance in doing the following: I have a system generated text file(a makefile basically). Before I can execute the make command, I need to modify one section of this generated file. The generated section is as follows: # INCLUDE macro... (5 Replies)
Discussion started by: innocentspirit
5 Replies

7. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies

8. Shell Programming and Scripting

Moving next 2 lines contents to previous lines

My input file is aaa bbb ccc a1a b1b c1c a2a b2b c2c I want the output file to look like that: aaa,bbb,ccc a1a,b1b,c1c a2a,b2b,c2c How do I achieve this ? (8 Replies)
Discussion started by: Amruta Pitkar
8 Replies

9. UNIX for Advanced & Expert Users

help in moving

can any one help i have a file with this content file name hsnvalue.op containing HSN=0,tool=123 HSN=0,tool=123 HSN=0,tool=123 HSN=0,tool=123 HSN=0,tool=123 HSN=0,tool=123 i want to print only one value to another file final.out :confused: (5 Replies)
Discussion started by: shafique
5 Replies

10. Shell Programming and Scripting

moving between vi files

would any one please tell me which keys we used between opened vi files backward and forward Thanks Ayah (2 Replies)
Discussion started by: aya_r
2 Replies
Login or Register to Ask a Question