Merging Lines based on criteria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging Lines based on criteria
# 1  
Old 03-29-2012
Merging Lines based on criteria

Hello,

Need help with following scenario.

A file contains following text:

Code:
{beginning of file}
New: This is a new record and it is not
on same line. Since I have lost touch with script
take this challenge and bring all this in one line.
New: Hello losttouch. You seem to be struggling with 
this really simple challenge.
New: It is better that I ask some expert on unix.com
They will show you this is simple enough.
{end of file}

What I am trying to accomplish is to get all the lines starting from "New:" until next "New:" is read; on to one line. Resultant lines should be like:

New: This is a new record and it is not on same line. Since I have lost touch with script take this challenge and bring all this in one line.
New: Hello losttouch. You seem to be struggling with this really simple challenge.
New: It is better that I ask some expert on unix.com.They will show you this is simple enough.

As I mentioned in the above statements, I have lost touch with this, I am really struggling to accomplish this task. Any help is greatly appreciated.

Thanks so much.

EDIT: Needless to say that if I read next "New:" (lets say in any variable) it implies, my previous record is complete and should be displayed or appended to a file.

Last edited by Scrutinizer; 03-29-2012 at 09:23 AM.. Reason: Please Use [code] tags [/code], thank you..
# 2  
Old 03-29-2012
Welcome to the forum.

Code:
perl -ne 'chomp; if(/^New:/){print "\n$_ ";$f=1}elsif(!/^New:/ && $f==1){print "$_ "}' inputfile

A different logic:
Code:
perl -ne 'chomp; if(/^New:/){print "$x\n"; $x="$_ "}else{$x .= "$_ "} END {print "$x\n"}' inputfile

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 03-29-2012
Code:
awk 'NR>1{gsub(/^New/,"\nNew")}END{printf "\n"}1' ORS=' ' infile


Last edited by ctsgnb; 03-29-2012 at 09:26 AM..
This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 03-29-2012
Code:
tr '\n' ' ' < infile | sed 's/ New/\nNew/g'

This User Gave Thanks to complex.invoke For This Post:
# 5  
Old 04-10-2012
Thank you everyone for the help! It is greatly appreciated. I used the awk statement and it worked really well. One edit I made to it was by changing the ORS from ' ' to '' (no space). I had to remove space because it was introduced space in and was making the entries less "meaningful".

Thanks once again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove lines from File.A based on criteria in File.B

Hello, I have two files of the following form. I would like to remove from File.A where the first three colum matches values in File.B to give the output in File.C File.A 121 54321 PQR CAT 122 765431 ABC DOG 124 98765 ZXY TIGER 125 86432 GEF LION File.B 122 765431 ABC 125 86432 GEF... (4 Replies)
Discussion started by: Gussifinknottle
4 Replies

2. UNIX for Beginners Questions & Answers

Merging multiple lines into single line based on one column

I Want to merge multiple lines based on the 1st field and keep into single record. SRC File: AAA_POC_DB.TAB1 AAA_POC_DB.TAB2 AAA_POC_DB.TAB3 AAA_POC_DB.TAB4 BBB_POC_DB.TAB1 BBB_POC_DB.TAB2 CCC_POC_DB.TAB6 OUTPUT ----------------- 'AAA_POC_DB','TAB1','TAB2','TAB3','TAB4'... (10 Replies)
Discussion started by: raju2016
10 Replies

3. Shell Programming and Scripting

Merging 2 lines based on a string

Dear Unix gurus I need help with a command or script to merge 2 lines where ever we find the string. I have attached scanned document. First line has string value: VSIN, immediate line has value: SETTLEMENT Where it finds the 2 string values in the whole file, one below the other,... (8 Replies)
Discussion started by: Karunyam
8 Replies

4. UNIX for Dummies Questions & Answers

Merging lines based on one column

Hi, I have a file which I'd like to merge lines based on duplicates in one column while keeping the info for other columns. Let me simplify it by an example: File ESR1 ANASTROZOLE NA FDA_approved ESR1 CISPLATIN NA FDA_approved ESR1 DANAZOL agonist NA ESR1 EXEMESTANE NA FDA_approved... (3 Replies)
Discussion started by: JJ001
3 Replies

5. Shell Programming and Scripting

Need To Delete Lines Based On Search Criteria

Hi All, I have following input file. I wish to retain those lines which match multiple search criteria. The search criteria is stored in a variable seperated from each other by comma(,). SEARCH_CRITERIA = "REJECT, DUPLICATE" Input File: ERROR,MYFILE_20130214_11387,9,37.75... (3 Replies)
Discussion started by: angshuman
3 Replies

6. Shell Programming and Scripting

Select lines from a file based on a criteria

Hi I need to select lines from a txt file, I have got a line starting with ZMIO:MSISDN= and after a few line I have another line starting with 'MOBILE STATION ISDN NUMBER' and another one starting with 'VLR-ADDRESS' I need to copy these three lines as three different columns in a separate... (3 Replies)
Discussion started by: Tlcm sam
3 Replies

7. Shell Programming and Scripting

Delete new lines based on search criteria

Hi all! A bit of background: I am trying to create a script that formats SQL statements. I have gotten so far as to add new lines based on certain match criteria like commas, keywords etc. In the process, I end up adding newlines where I don't want. For example: substr(colName, 1, 10)... (3 Replies)
Discussion started by: jayarkay
3 Replies

8. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

9. Shell Programming and Scripting

remove lines based on score criteria

Hi guys, Please guide for Solution. PART-I INPUT FILE (has 2 columns ID and score) TC5584_1 93.9 DV161411_2 79.5 BP132435_5 46.8 EB682112_1 34.7 BP132435_4 29.5 TC13860_2 10.1 OUTPUT FILE (It shudn't contain the line ' BP132435_4 29.5 ' as BP132435 is repeated... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies

10. Shell Programming and Scripting

Merging lines based on occurances of a particular character in a file

Hi, Is there any way to merge two lines based on specific occurance of a character in a file. I am having a flat file which contains multiple records. Each row in the file should contain specified number of delimiter. For a particular row , if the delimiter count is not matched with... (2 Replies)
Discussion started by: mohan_tuty
2 Replies
Login or Register to Ask a Question