To print from the first line until pattern is matched


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users To print from the first line until pattern is matched
# 8  
Old 02-11-2015
With awk:
Code:
awk '{print; if ($1=="LAC") exit}' file

Code:
awk '{print; if ($2=="LAC") exit}' file

Code:
awk '{print; if ($1=="LAC" || $2=="LAC") exit}' file

# 9  
Old 02-11-2015
But there is one catch in the case of sed. If the match is on the first line, then it will not work.. To handle this case you need a GNU extension (GNU sed):
Code:
gsed -n '0,/\<LAC\>/p' file

awk does not have this problem and can also use a range expression and also can have an exact field match as noted by MadeInGermany, so you do not need to delimit the search:
Code:
awk 'NR==1, $1=="LAC"||$2=="LAC"' file

or
Code:
awk -v s=LAC 'NR==1, $1==s||$2==s' file


MadeinGermany's approach with the exit statement would be more efficient.

--
This exit approach would also work with regular sed and this does not have the match-on-line-1 catch:
Code:
sed '/^LAC$/q; /^LAC[[:blank:]]/q; /[[:blank:]]LAC[[:blank:]]/q' file


Last edited by Scrutinizer; 02-12-2015 at 12:30 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find matched pattern and print all based on certain conditions

Hi, I am trying to extract data based on certain conditions. My sample input file as below:- lnc-2:1 OnePiece tra_law 500 688 1 . . g_id "R792.8417"# tra_law_id "R792.8417.1"# g_line "2.711647"# KM "8.723820"# lnc-2:1 OnePiece room 500 510 1 . . g_id "R792.8417"# tra_law_id "R792.8417.1"#... (7 Replies)
Discussion started by: bunny_merah19
7 Replies

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

3. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

4. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

5. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

6. Shell Programming and Scripting

Print only matched pattern in perl

Hi, I have script like below: #!/usr/local/bin/perl use strict; use warnings; while (<DATA>) { ( my ($s_id) = /^\d+\|(\d+?)\|/ ) ; if ( $s_id == 1 ){ s/^(.*\|)*.*ABC\.pi=(+|+)*.*ABC\.id=(\d+|+).*$/$1$2|$3/s; print "$1$2|$3\n"; (2 Replies)
Discussion started by: sol_nov
2 Replies

7. Shell Programming and Scripting

print the whole row in awk based on matched pattern

Hi, I need some help on how to print the whole data for unmatched pattern. i have 2 different files that need to be checked and print out the unmatched patterns into a new file. My sample data as follows:- File1.txt Id Num Activity Class Type 309 1.1 ... (5 Replies)
Discussion started by: redse171
5 Replies

8. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

9. Shell Programming and Scripting

print last matched pattern using perl

Hi, If there exist multiple pattern in a file, how can I find the last record matching the pattern through perl. The below script searches for the pattern everywhere in an input file. #! /usr/bin/perl -s -wnl BEGIN { $pattern or warn"Usage: $0 -pattern='RE' \n" and exit 255;... (5 Replies)
Discussion started by: er_ashu
5 Replies

10. Shell Programming and Scripting

SED: delete and print the only exact matched pattern

I am really need help with the regular expression in SED. From input file, I need to extract lines that have the port number (sport or dport) as defined. The input file is something like this time=1209515280-1209515340 dst=192.168.133.202 src=208.70.8.23 bytes=2472 proto=6 sport=80 dport=1447... (6 Replies)
Discussion started by: new_buddy
6 Replies
Login or Register to Ask a Question