Join in a single line variable number of lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join in a single line variable number of lines
# 1  
Old 08-03-2009
Join in a single line variable number of lines

Hi all,

I have a file with little blocks beginning with a number 761XXXXXX, and 0, 1, 2 or 3 lines below of it beginning with STUS as follow:

Code:
761625820
STUS ACTIVE 16778294
STUS NOT ACTIVE
761157389
STUS ACTIVE 16778294
761554921
STUS ACTIVE 16778294
STUS NOT ACTIVE
STUS ACTIVE OP
761229934
761335544
STUS ACTIVE 16778294

I was trying to join the blocks in a single line as follow desired output:
Code:
761625820 STUS ACTIVE 16778294 STUS NOT ACTIVE
761157389 STUS ACTIVE 16778294 
761554921 STUS ACTIVE 16778294 STUS NOT ACTIVE STUS ACTIVE OP
761229934
761335544 STUS ACTIVE 16778294

I tried with something I thinked, but doesn`t seem to work.

Code:
awk '{while ($0 ~ /^761/) {getline temp; $0 = $0 temp} print $0}' inputfile

Maybe somebody could help me to get the desired output.

Thanks in advance,

Best regards.
# 2  
Old 08-03-2009

Code:
awk '/^761/ {
   if ( NR > 1 ) print line
   line = $0
   next
 }
 { line = line " " $0 }
' "$file"

# 3  
Old 08-03-2009
Many thanks Mr. cfajohnson. Very appreciated your help. It works exactly like I was looking for.

One more thing.

May you or somebody please explain a little bit how your code work?

Thanks again.
# 4  
Old 08-03-2009
Code:
sed -n '/^761/{
1{h;}
1!{x;s/\n//g;p;}
}
/^761/!{
${H;x;s/\n//g;p;}
$!{H;}
}'

# 5  
Old 08-03-2009
Quote:
Originally Posted by summer_cherry
Code:
sed -n '/^761/{
1{h;}
1!{x;s/\n//g;p;}
}
/^761/!{
${H;x;s/\n//g;p;}
$!{H;}
}'

I have the same pb : joining 3 lines in a data file when they belong to a same sequence number :
Quote:
(10)This is a sequence of data..
which continues in this line
and in this one also
(11)This a new sequence of data in one line
(12)This is new sequence of data in 3 lines
it continues here
and here
like that :

Quote:
(10)This is a sequence of data..,which continues in this line,and in this one also
(11)This a new sequence of data in one line
(12)This is new sequence of data in 3 lines,it continues here,and here
Your suggestion seems being exactly what I try to have (for 3 hours now, I am a very beginner wth sed.. Smilie)
but could you please detail the explanation ?
instead of trying to match lines not starting with "761" I want lines not starting with a sequence number between parenthesis so I replaced the
Code:
sed -n '/^761/{

with
Code:
sed -n '/^\([0-9]*\)/{

but I dont succeed in understanding the rest of the code

Thanks for your help !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to join separate lines in a single one from xml file

Hi all, I need help to parse this xml file that has paragraphs broken in different lines and I would like to join in a single line. I hope you can understand my explanation. Thanks for any help/direction. The script could be in bash, awk, ruby, perl whatever please In the output I want:... (8 Replies)
Discussion started by: Ophiuchus
8 Replies

2. Shell Programming and Scripting

Join two lines into one, but the second line only the last two columns

Hi guys, I hope you are doing well! I have a file and I need to join two lines into one, but the second line I need only the last two columns. ================= "eHealth Trend Report","logoRpt" "LAN/WAN Group 123" "Divide by Time" "switch1_a-RH-Serial0" "BW: 1.02 M" ... (4 Replies)
Discussion started by: antoniorajr
4 Replies

3. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

4. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

5. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

6. Shell Programming and Scripting

How to use Perl to join multi-line into single line

Hello, Did anyone know how to write a perl script to merge the multi-line into a single line where each line with start at timestamp Input--> timestamp=2009-11-10-04.55.20.829347; a; b; c; timestamp=2009-11-10-04.55.20.829347; aa; bb; cc; (5 Replies)
Discussion started by: happyday
5 Replies

7. Shell Programming and Scripting

Break lines up into single lines after each space in every line

It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space. Example of text file; line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)
Discussion started by: lewk
7 Replies

8. Shell Programming and Scripting

join lines on line break in files

i had a file where lines appear to be broken when they shouldn't eg Line 1. kerl abc sdskd sdsjkdlsd sdsdksd \ Line 2. ksdkks sdnjs djsdjsd i can do a shift join to combine the lines but i there are plenty of files with this issue Line 1. kerl abc sdskd sdsjkdlsd sdsdksd ksdkks sdnjs... (6 Replies)
Discussion started by: mad_man12
6 Replies

9. Shell Programming and Scripting

Edit number of lines in a file to single line

Greetings, I have a file: hostnames.txt which has - # cat hostnames.txt machine1 machine2 I need the output to be saved to a variable as: HOSTNAMELIST=machine1,machine2 Please advise. Thanks, Chiru (3 Replies)
Discussion started by: chiru_h
3 Replies

10. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question