Visit Our UNIX and Linux User Community


How to combine lines within range of pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to combine lines within range of pattern
# 1  
Old 09-05-2009
How to combine lines within range of pattern

I've a file say having
line 1
line 2
(NP
line 3
line 4
line 5)
line 6


I want to combine lines starting from (NP and ending with ) then it will look like
line 1
line 2
(NP line3 line4 line5)
line 6


I tried using
Code:
sed '/(NP/,/)$/ s/\n/ /'

but it's not working. Any help please?

Thanks in advance.
# 2  
Old 09-05-2009

Code:
awk '
{printf "%s ", $0}
/)$/ { print "" }
/\(/,/\)$/ { next }
{print ""}'

# 3  
Old 09-05-2009
Another way:
Code:
awk '/NP/ {ORS=" "} /\)$/ {ORS="\n"}{print} file'

# 4  
Old 09-05-2009
Thanks guys, it worked.
Cheers.
# 5  
Old 09-05-2009
Hi.

If you like the /first/, /last/ notation of sed, there is a similar construct in perl, namely the range operator, "..", and, in the context of lines, it works as you wanted the sed construct to work (it can also used as a list generator). The perl script is a bit more verbose than the awk scripts, but it may be more readable:
Code:
#!/usr/bin/perl

# @(#) p2	Demonstrate line-join within specific range.

use warnings;
use strict;

my ($debug);
$debug = 0;
$debug = 1;

my ($in_sequence) = 0;
while (<>) {
  if ( /[(]NP/ .. /[)]$/ ) {
    $in_sequence++;
    chomp;
    print "$_ ";
    next;
  }
  elsif ($in_sequence) {
    $in_sequence = 0;
    print "\n";
  }
  print;
}

exit(0);

Assuming data is on file "data1", the script then produces:
Code:
% ./p2 data1
line 1
line 2
(NP line 3 line 4 line 5) 
line 6

The parentheses are special in the matching operation, so we escape them. One way is to precede them with a backslash, but some people like readability of single characters in square brackets.

We go through the file and whenever we are in the appropriate range, we print the line without the newline, and move onto the next line. If outside, we check to see if we have completed a join, and if so, print a newline, print the current line in any case, and loop. There is also a triple-dot operator, research on which is left as an exercise for the reader.

Best wishes... cheers, drl
# 6  
Old 09-06-2009
Thanks drl, I appreciate your consideration. I had enuf reply from all of you.
Cheers up
# 7  
Old 09-06-2009
Yet another solution:

Code:
$ 
$ cat f1
line 1
line 2
(NP
line 3
line 4
line 5)
line 6
$ 
$ 
$ ##
$ perl -ne 'BEGIN{$x=0} chomp;
>           if (/^\(/){$x=1; $c=1}
>           elsif (/\)$/){$x=0; $c=0}
>           elsif ($x==1){$c=1}
>           else {$c=0}
>           if ($c == 1) {printf("%s ", $_)}
>           else {printf("%s\n", $_)}' f1
line 1
line 2
(NP line 3 line 4 line 5)
line 6
$ 
$

tyler_durden

Previous Thread | Next Thread
Test Your Knowledge in Computers #132
Difficulty: Easy
Many of the command line and graphical utilities in a Linux distro are very similar to a Unix system.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

awk to combine lines if fields match in lines

In the awk below, what I am attempting to do is check each line in the tab-delimeted input, which has ~20 lines in it, for a keyword SVTYPE=Fusion. If the keyword is found I am splitting $3 using the . (dot) and reading the portion before and after the dot in an array a. If it does have that... (12 Replies)
Discussion started by: cmccabe
12 Replies

4. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

5. Shell Programming and Scripting

sed Range Pattern and 2 lines before Start Pattern

Hi all, I have been searching all over Google but I am unable to find a solution for a particular result that I am trying to achieve. Consider the following input: 1 2 3 4 5 B4Srt1--Variable-0000 B4Srt2--Variable-1111 Srt 6 7 8 9 10 End (3 Replies)
Discussion started by: y2jacky
3 Replies

6. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

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

8. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

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

10. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

Featured Tech Videos