sed command to print first instance of pattern in range

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
# 1  
[solved] sed command to print first instance of pattern in range

The following text is in testFile.txt:
one 5
two 10
three 15
four 20
five 25
six 10
seven 35
eight 10
nine 45
ten 50

I'd like to use sed to print the first occurance of search pattern /10/ in a given range. This command is to be run against large log files, so to optimize efficiency, I'd like to use sed's q command to stop processing after reaching the first matching pattern. For example, to print the first occurance of /10/ in the range of lines from 2 to end-of-file, it should function like the following.

sed -n '2,${/10/p;}' testFile.txt | head -1

This will generate output of only the following line:
two 10

Sed version: installed with OS, AIX 5.3 TL 12.

Last edited by uschaafm; 02-12-2013 at 03:03 PM.. Reason: updating thread as solved.
# 2  
sed -n '2,${/10/!n; p; q; }' testFile.txt

Last edited by rdrtx1; 02-11-2013 at 08:40 PM.. Reason: corrected for not match at line 2 as in example posted, as allister pointed out.
# 3  
Just add the 'q;' option to quit after the first match:

sed -n '2,${/10/p;q;}' testFile.txt
two 10

# 4  
sed -n '/10/ {p;q}' file

# 5  
using for n grep

I'm nt cmfortable wid sed. Bt i thunk it can be done using for loop n grep. As i'm posting through mobile so code isn't testd. Plz modify to meet ur need
for $ln `cat testfile.txt`
  num=echo $ln | grep -Po *[0-9]{2}
 if [ $num = '10' ]
 echo $ln
exit 0

it'll search upto first occurance n then exit. Plz revrt incase of furthr explanation

Last edited by Franklin52; 02-13-2013 at 06:13 AM.. Reason: Please use code tags for data and code samples
# 6  
Originally Posted by rdrtx1
sed -n '2,${/10/p; q;}' testFile.txt

Originally Posted by in2nix4life
Just add the 'q;' option to quit after the first match:

sed -n '2,${/10/p;q;}' testFile.txt
two 10

Your suggestions unconditionally abort after reading line 2, and will generate incorrect output if the first instance of matching text within the range does not occur on the first line of that range.

Originally Posted by shamrock
sed -n '/10/ {p;q}' file

This suggestion does not implement the range restriction.

A correct, sed-only solution:
sed -n '2,${/10/!d; p; q;}'

If the end of the range is not the end of the file, then an additional check is needed to avoid reading the remainder of a long file which does not match. Taking the range to be 2,50:
sed -ne '2,50{/10/!d; p; q;}' -e '50q'


Last edited by alister; 02-11-2013 at 07:33 PM..
This User Gave Thanks to alister For This Post:
# 7  
No point in printing before or reading after:
sed '

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #620
Difficulty: Medium
Python implicitly assumes that each physical line corresponds to a logical line.
True or False?

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

sed to grab first instance of a range

Hi all, I'm new to the forum and also relatively new to sed and other such wonderfully epic tools. I'm attempting to grab a section of text between two words, but it seems to match all instances of the range instead of stopping at just the first. This occurs when I use: sed -n... (7 Replies)
Discussion started by: Lazarix
7 Replies

4. Shell Programming and Scripting

sed for first instance of a pattern

Hi Everyone, I have the below information from a log file: LOAD SUMMARY ============ WRT_8036 Target: TGT_1_TAB (Instance Name: ) WRT_8039 Inserted rows - Requested: 3929 Applied: 0 Rejected: 3929 Affected: 0 Mutated from update: 3929 WRT_8041 Updated rows ... (7 Replies)
Discussion started by: galaxy_rocky
7 Replies

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

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

7. Shell Programming and Scripting

Print pattern range to a new file

Hi Everyone! I really appreciate all of your help, I'm learning so much, can't wait until I get good enough to start answering questions! I have a problem ... from one large file, I'd like to create multiple new files for each pattern block beginning with /^ISA/ ending with /^IEA/ ... (2 Replies)
Discussion started by: verge
2 Replies

8. Shell Programming and Scripting

sed: how to limit pattern search to first instance only

I need to reduce a file's size below 50MB by deleting chucks of text. The following sed does this. sed '/^begpattern/,/endpattern/d' myfile However, it's possible that the file size can get below 50MB by just deleting the first instance of the pattern. How do I code that into sed? Or can awk... (8 Replies)
Discussion started by: mariod1049
8 Replies

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

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