how to combine 2 lines in same files based on any text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to combine 2 lines in same files based on any text
# 1  
Old 07-06-2009
how to combine 2 lines in same files based on any text

hi,

I want to combine two lines in same file. If the line ends with '&' it should belongs to previous line only
Here i am writing example.

Ex1:
line 1 : return abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz

output should be
line 1: return abcdefghijklmnopqrstuvwxyz

joint based on '&' .
Thanks in Advance
# 2  
Old 07-06-2009
Added some lines as example to show it will not append other lines below:
Code:
$> cat infile
abcdefgh&
ijklmnopqr&
stuvw&
xyz
some
more bla
$> sed -e :a -e '/&$/ {N; s/&\n//}; ta' infile
abcdefghijklmnopqrstuvwxyz
some
more bla

# 3  
Old 07-06-2009
Try this,

cat <Filename> | xargs -E '&' | sed 's/& //g'
# 4  
Old 07-06-2009
why is xyz joined together then?
# 5  
Old 07-06-2009
HI ramesh SRK,

Thank you. "cat <Filename> | xargs -E '&' | sed 's/& //g'" this is working. But still some problem... But some problem.
here i ll expalin with example

line 1 : abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz

Ur script output : abcdefghijklmnopqrstuvwxyz.

but
line 1 : return abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz
line 5 : lalalala

then output should come
line 1 : abcdefghijklmnopqrstuvwxyz
line 2 : lalala

but evrything is coming in a single line
line 1 : abcdefghijklmnopqrstuvwxyzlalala

please help me ....
# 6  
Old 07-06-2009
Did you ignore my answer? It does exactly what you asked now the 2nd time. Smilie I just forgot to add the word "return" in the first line but it doesn't matter generally and can be added. Result should be the same.

Edit:
Don't want to sound rude, just wondering.

Last edited by zaxxon; 07-06-2009 at 09:14 AM..
# 7  
Old 07-06-2009
Quote:
Originally Posted by zaxxon
Did you ignore my answer? It does exactly what you asked now the 2nd time. Smilie I just forgot to add the word "return" in the first line but it doesn't matter generally and can be added. Result should be the same.
sorry Zaxxon....I am not much familiar with shell scripting. Just learning now. I dont know exact use of "sed" command. I just copied ur code and tried here . :-)
But error coming "sed: command garbled: /&$/ {N; s/&\n//}; ta"

I couldnt fix this error.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine and complete multiple CSV files based on 1 parameter

I have to create a new CSV file based on the value listed on the 3rd column from different CSV files. This is what I need: 1. I should substitute the first column from each file, excluding the headers, with the file name InputXX. 2. Then, I need to look for rows with 0 on the third column in... (7 Replies)
Discussion started by: Xterra
7 Replies

2. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Awk: Combine multiple lines based on number of fields

If a file has following kind of data, comma delimited 1,2,3,4 1 1 1,2,3,4 1,2 2 2,3,4 My required output must have only 4 columns with comma delimited 1,2,3,4 111,2,3,4 1,222,3,4 I have tried many awk command using ORS="" but couldnt progress (10 Replies)
Discussion started by: mdkm
10 Replies

4. Shell Programming and Scripting

Combine 3 files based on a pattern

HI, I have 3 files that contain the following information (sql output from Oracle database stored in a txt file): File1.txt : alter table "SYS"."INT_COST_PRICE" enable row movement; alter table "SYS"."INT_SOH" enable row movement; alter table "SYSMAN"."XX_ACI_SKURTP" enable row movement;... (6 Replies)
Discussion started by: rparavastu
6 Replies

5. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

6. Shell Programming and Scripting

Combine multiple lines in file based on specific field

Hi, I have an issue to combine multiple lines of a file. I have records as below. Fields are delimited by TAB. Each lines are ending with a new line char (\n) Input -------- ABC 123456 abcde 987 890456 7890 xyz ght gtuv ABC 5tyin 1234 789 ghty kuio ABC ghty jind 1234 678 ght ... (8 Replies)
Discussion started by: ratheesh2011
8 Replies

7. Shell Programming and Scripting

Combine the lines from separate text files

Hi All, I have three separate text files which has only one line and i want to combine these lines in one text file which will have three lines. cat file1.txt abc cat file2.txt 1265 6589 1367 cat file3.txt 0.98 0.36 0.5 So, I want to see these three lines in the... (9 Replies)
Discussion started by: senayasma
9 Replies

8. Shell Programming and Scripting

combine lines from two files based on an if statement

I'm rather new to programming, and am attempting to combine lines from 2 files in a way that is way beyond my expertise - any help would be appreciated! I need to take a file (file1) and add columns to it from another file (file2). However, a line from file2 should only be added to a given line... (3 Replies)
Discussion started by: Cheri
3 Replies

9. Shell Programming and Scripting

How to combine two files based on fields?

I have two files which are as follows: File 1: 1 abc 250 2 pqr 300 3 xyz 100 File 2: 1 abc 230 2 pqr 700 3 xyz 500 Now I need output File, File 3as: S.No Name Count1 Count2 1 abc 250 230 2 pqr 300 700 3 xyz 100 500 NOTE: (13 Replies)
Discussion started by: karumudi7
13 Replies

10. Shell Programming and Scripting

Combine the lines based on particular pattern

Hi, I've a weird problem to be solved. Assume i have a file like this: 1. <timestamp> UID: 12345 <junk> DevID: V123 2. <timestamp>DevID: V123 <junk> DuID: VP 3. ... 4. .... 5. <timestamp> UID: 789 <junk> DevID: S456 6. <timestamp>DevID: S456 <junk> DuID: VP.... 7. ..... Say if i... (3 Replies)
Discussion started by: VenkataPrasad
3 Replies
Login or Register to Ask a Question