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
# 8  
Old 07-06-2009
An approach with awk:

Code:
awk '/&$/{sub("&$","");printf $0;next}1' file

# 9  
Old 07-06-2009
Ok Smilie For the sed thing you might try to use GNU sed if available on your OS.
# 10  
Old 07-07-2009
Quote:
Originally Posted by zaxxon
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



hi Zaxxon,

ur script is working only when i give like below format
CASE 1:
sed -e :a -e '/&$/ {
> N
> s/&\n//
> }
> ta' infile
But if i give

CASE 2:
" sed -e :a -e '/&$/ {N; s/&\n//}; ta' infile"
can u tel me what was the problem for above line?
I tried different different cases...

CASE 3:
"sed -e :a -e '/&$/ { N; s/&\n//;}; ta' infile"
above line gives output only first iteration
that means it checks '&' only once.


$> cat infile
abcdefgh&
ijklmnopqr&
stuvw&
xyz
some
more bla

OUTPUT coming like this
abcdefghijklmnopqr&
stuvwxyz
some
more bla

NOTE : Im using solaris...gsed command not there..

thanks in advance.......

# 11  
Old 07-07-2009
At first:
Please use CODE-tags when you display code, data or logs. It really enchances readability, formatting and preserves special characters. It could happen that you might get a warning or a charge on bits of your forum account if you proceed like that Smilie

Hm...
:a is a label and as long as the condition is true ie. the last character in the line is a & it should start over at the label :a. :ta just says "if-true-go-to-a.

I am not sure but I have something similar on AIX where I have only a plain sed. It seems it is expecting the commands inside the curled bracket one per line. Here the problem I get like you got:
Code:
> sed -e :a -e '/&$/ {N; s/&\n//}; ta' infile
sed: 0602-404 Function /&$/ {N; s/&\n//}; ta cannot be parsed.

Here changed so it is working:
Code:
$> sed -e :a -e '/&$/ {
> N
> s/&\n//
> }
> ta' infile
abcdefghijklmnopqrstuvwxyz
some
more bla

# 12  
Old 07-07-2009
Code:
undef $/;
$str=<DATA>;
my @arr=split(/(?:[^&])\n/,$str);
map {s/&[^:]*:\s*//g} @arr;
print join "\n", @arr;
__DATA__
line 1 : return abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz
line 1 : return abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz
line 1 : return abcdefgh&
line 2 : ijklmnopqr&
line 3 : stuvw&
line 4 : xyz

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