Print two lines according second column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print two lines according second column
# 1  
Old 03-24-2010
Print two lines according second column

hi I would like to print those lines and their next lines which begin with ( in second column

example

input

Code:
panda       (-,xxx,xxx.co.uk)\
12
patrols         patrols1\
2
patrols1         (xxx,-,xxx.co.uk)\
4
patrols2         (-,xxx,xxx.co.uk)\
14
canada_patrol       (-,xxx,xxx.co.uk)\
12
simpsons      (-,xxx,xxx.co.uk)\
2
cool         cool1\
2
cool1        (-,xxx,xxx.co.uk)\
18


desired output:
Code:
panda       (-,xxx,xxx.co.uk)\
12
patrols1         (xxx,-,xxx.co.uk)\
4
patrols2         (-,xxx,xxx.co.uk)\
14
canada_patrol       (-,xxx,xxx.co.uk)\
12
simpsons      (-,xxx,xxx.co.uk)\
2
cool1        (-,xxx,xxx.co.uk)\
18



I tried so far:

Code:
awk  '{ if ( $2~"^\\(.*" ) printf $0; getline; print $0 }' file

but did not work.

Thanks a lot
# 2  
Old 03-24-2010
Code:
sed -n '/.[^(]*[ \t]*(/{p;n;p}' file

# 3  
Old 03-24-2010
Hi, wakatana:

Your attempt was close. Changing the printf to a print and enclosing the statements within curly braces to make them all part of the if statement's block fixes it:
Code:
awk  '{ if ( $2~"^\\(.*" ) {print $0; getline; print $0} }'

However, you could do away with the if statement and just use AWK's native pattern-action syntax:
Code:
awk  '$2~"^\\(.*" {print $0; getline; print $0 }'

If you like, you can simplify it a bit by using a regular expression literal instead of a string (so as to avoid having to escape through the string parsing layer before arriving at the regular expression parser:
Code:
awk  '$2~/^\(/ {print; getline; print}'

Regards,
Alister

---------- Post updated at 03:45 AM ---------- Previous update was at 03:38 AM ----------

Quote:
Originally Posted by kurumi
Code:
sed -n '/.[^(]*[ \t]*(/{p;n;p}' file

This can fail if there is an open parenthesis in the first field. The negated open parenthesis class need not occur and the same goes for the blank class with the space and tab, allowing the literal "(" in the pattern to fall within the first field.

A much better approach would be:
Code:
sed -n '/^[^ ]*  *(/{p;n;p;}' file

Regards,
Alister

Last edited by alister; 03-24-2010 at 04:55 AM..
# 4  
Old 03-24-2010
Thanks a lot guys, you helped Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

Search Pattern and Print lines in Single Column

Hi Experts I have small query where I request the into a single file Suppose: File1: {Unique entries} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 Replies

3. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Print lines that contain a value in a specific column shared by more than 1 entity in another col

I want to expand on a question that I just asked here: I want to extract only those values in Column 2 that are shared by at least 2 unique values in Column 2. Using the same input (in this case 3- tab-separated columns): waterline-n below-sheath-v 14.8097 dock-n below-sheath-v ... (2 Replies)
Discussion started by: owwow14
2 Replies

6. Shell Programming and Scripting

Based on column in file1, find match in file2 and print matching lines

file1: file2: I need to find matches for any lines in file1 that appear in file2. Desired output is '>' plus the file1 term, followed by the line after the match in file2 (so the title is a little misleading): This is honestly beyond what I can do without spending the whole night on it, so I'm... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

7. Shell Programming and Scripting

awk print non matching lines based on column

My item was not answered on previous thread as code given did not work I wanted to print records from file2 where comparing column 1 and 16 for both files find rows where column 16 in file 1 does not match column 16 in file 2 Here was CODE give to issue ~/unix.com$ cat f1... (0 Replies)
Discussion started by: sigh2010
0 Replies

8. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

9. Shell Programming and Scripting

Strings from one file which exactly match to the 1st column of other file and then print lines.

Hi, I have two files. 1st file has 1 column (huge file containing ~19200000 lines) and 2nd file has 2 columns (small file containing ~6000 lines). ################################# huge_file.txt a a ab b ################################## small_file.txt a 1.5 b 2.5 ab ... (4 Replies)
Discussion started by: AshwaniSharma09
4 Replies

10. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies
Login or Register to Ask a Question