Sed: how to merge two lines moving matched pattern to end of previous line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed: how to merge two lines moving matched pattern to end of previous line
# 1  
Old 09-13-2014
Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone,
im new here, and also programming with awk, sed and grep commands on linux.
In my text i have many lines with this config:
Code:
1  1  4  3  1  1  2  5
2  2  1  1  1  3  1  2
1  3  1  1  1  2  2  2
            5  2  4  1
3  2  1  1  4  1  2  1
1  1  3  2  1  1  5  4
1  3  1  1  1  3  3  1
            1  2  2  5
1  2  3  1  5  4  1  1

I wanna move the numbres (separated by 2 spaces) in a line after 12 consecutive spaces to the end of the previous line, resulting in the following:

Code:
1  1  4  3  1  1  2  5
2  2  1  1  1  3  1  2
1  3  1  1  1  2  2  2  5  2  4  1
3  2  1  1  4  1  2  1
1  1  3  2  1  1  5  4
1  3  1  1  1  3  3  1  1  2  2  5
1  2  3  1  5  4  1  1

Can you please help?

Thanks in advance for your help!

Julien
Moderator's Comments:
Mod Comment Please use CODE (not ICODE) tags for multi-line sample input, output, and code segments.

Last edited by Don Cragun; 09-13-2014 at 03:01 PM.. Reason: Fix CODE and ICODE tags.
# 2  
Old 09-13-2014
Here's a pe(a)rl ;-)

Code:
perl -lne 'chomp;if($.==1){$p=$_;next}; if($_=~/^\s+(.*)$/){$p = $p." ".$1}else{print $p; $p=$_} END{print $p}' file

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 09-13-2014
It works! many thanks
# 4  
Old 09-14-2014
The thread title asks for a sed solution. Here is one:
Code:
sed '
/^    /{H;x;s/\n    */  /;$p;x;d;}
x
1d
${p;x;}
' file

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

2. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

3. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

4. Shell Programming and Scripting

sed: how to move matched pattern to end of previous line

Hello, I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help. I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 Replies

5. Shell Programming and Scripting

Sed: Working on a line Previous to a pattern.

Hello everyone, I am working with some train time tables, and i have hit a bit of a road block. Using grep/sed i have done a reasonable job of parsing the html into comma delimited format, but NJ transit prints The Track number and status on a new line, and I would much prefer it all on a... (6 Replies)
Discussion started by: mussen
6 Replies

6. Shell Programming and Scripting

Printing previous line based on pattern using sed

Hi, I have a written a shell script to get the previous line based on the pattern. For example if a file has below lines: ---------------------------------------------- #UNBLOCK_As _per #As per 205.162.42.92 #BLOCK_As_per #----------------------- #input checks abc.com... (5 Replies)
Discussion started by: Anjan1
5 Replies

7. Shell Programming and Scripting

extracting matched pattern from a line using sed

I am trying to pull certain pieces of data out of a line of a file that matches a certain pattern: The three pieces that I want to pull out of this line are the only occurrences of that pattern within the line, but the rest of the line is not consistent in each file. Basically the line is... (3 Replies)
Discussion started by: ellhef
3 Replies

8. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

9. Shell Programming and Scripting

Removing end of line to merge multiple lines

I'm sure this will be an easy question for you experts out there, but I have been searching the forum and working on this for a couple hours now and can't get it right. I have a very messy data file that I am trying to tidy up - one of the issues is some records are split into multiple lines: ... (4 Replies)
Discussion started by: tink
4 Replies

10. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies
Login or Register to Ask a Question