Visit The New, Modern Unix Linux Community


Merging 2 lines based on a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging 2 lines based on a string
# 1  
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, those 2 lines to be merged into a single line and the other detail data lines has to be remained in the same order.


E.g.:

Code:
Line1: VSIN .... (Space) SETTLEMENT CURRENCY ....
Line2: other detail data in the same order 
Line3: VJRE ...
Line4: VJRE ...
Line5: VSIN .... (Space) SETTLEMENT CURRENCY ....
Line6: VJRE ....
Line7: VJRE ....
Line8: VJRE...
Line9: VSIN. ...(Space) SETTLEMENT CURRENCY....
....
....

Many thanks
Your help is much appreciated as I am not a core Unix programmer.
Many thanks
Karunya
Merging 2 lines based on a string-imagejpg

Last edited by Scrutinizer; 02-05-2015 at 01:10 AM.. Reason: code tags, removed quote tags
# 2  
Hi what have you tried ? Also please post a sample input, rather than attach a picture..
# 3  
Code:
Line1:vsin : vj005 Acc type : vjs
Line2:Settlement cur : USD trade date :22-01-12
Line3:vjre 15565  1 020765 S  30-01-14 12,000 Vets
Line4: vjre 12678  1 03765   B 30-01-14  2,000 vets
Line5: vsin : vj006 Acc type : cgf
Line6: vjre  1025   1 07654    S 30-01-14. 2,765 vets
..
..

---------- Post updated at 04:28 AM ---------- Previous update was at 04:26 AM ----------

Sorry line 6 is just like line 2 which starts with
Line6: Settlement curr ...
[/CODE]
# 4  
Well, that's sort of sample input, more or less. What about scrutinizer's first question?
# 5  
Googled for the command to get solution and I am not able find one. Your help is much appreciated.
# 6  
OK. Try
Code:
sed '/^VSIN/ {N; s/\n/ /}' file

or

Code:
awk '/^VSIN/ {getline X; $0=$0 " " X}1' file


Please be aware that this assumes that line1: etc. is added by you and not in the file.

---------- Post updated at 14:33 ---------- Previous update was at 14:32 ----------

AND it is NOT clear if vsin is upper or lower case...
# 7  
Quote:
Thanks RudiC. But both the commands gave me the same output which is same as input file. It has not done any processing. Am i doing wrong some where ?.
I tried different command at office and it helped me to some extent(survived at the moment). will let you know about it tommorrow. as i do not have the command handy.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #691
Difficulty: Medium
UnixWare is a Unix operating system originally released by Univel.
True or False?

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Remove lines in a positional file based on string value

Gurus, I am relatively new to Unix scripting and am struck with a problem in my script. I have positional input file which has a FLAG indicator in at position 11 in every record of the file. If the Flag has value =Y, then the record from the input needs to be written to a new file.However if... (3 Replies)
Discussion started by: gsam
3 Replies

5. Shell Programming and Scripting

Merging Lines based on criteria

Hello, Need help with following scenario. A file contains following text: {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... (4 Replies)
Discussion started by: losttouch
4 Replies

6. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

7. Shell Programming and Scripting

Multi line document to single lines based on occurance of string

Hi Guys, I am new to awk and sed, i am working multiline document, i want to make make that document into SINGLE lines based on occurace of string "dwh". here's the sample of my problem.. dwh123 2563 4562 4236 1236 78956 12394 4552 dwh192 2656 46536 231326 65652 6565 23262 16625623... (5 Replies)
Discussion started by: victor369
5 Replies

8. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

9. 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

10. Shell Programming and Scripting

searching and storing unknown number of lines based on the string with a condition

Dear friends, Please help me to resolve the problem below, I have a file with following content: date of file creation : 12 feb 2007 ==================== = name : suresh = city :mumbai #this is a blank line = date : 1st Nov 2005 ==================== few lines of some text this... (7 Replies)
Discussion started by: swamymns
7 Replies

Featured Tech Videos