How to get a 1st line which matches the particular pattern?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get a 1st line which matches the particular pattern?
# 1  
Old 01-13-2016
How to get a 1st line which matches the particular pattern?

Hi all,

I have file on which I do grep on "/tmp/data" then I get 5 lines as

Code:
dir Path: /tmp/data/20162343134
Starting to listen on ports logging: 
--
Moving results files from local storage: /tmp/resultsFiles/20162343134/*.gz to NFS: /data/temp/20162343134/outgoing

from above got to get 1st line dir Path: /tmp/data/20162343134

thanks
--girija

Last edited by Don Cragun; 01-13-2016 at 04:14 AM.. Reason: Change B tags to CODE and ICODE tags.
# 2  
Old 01-13-2016
Why should you get above 5 lines from a grep "/tmp/data"? Please show your command and a small but representative sample.
# 3  
Old 01-13-2016
It would be interesting to see what grep command produced those four lines of output. And, if you think there are five lines there, it would be nice if you also showed us the other line.

But, depending on the options and operands you fed to grep to get that output, it would seem that unless other lines in /tmp/data contain the string Path:, you could get what you want just by using:
Code:
grep -F "Path:" /tmp/data

# 4  
Old 01-13-2016
Quote:
Originally Posted by girijajoshi
I have file on which I do grep on "/tmp/data" then I get 5 lines as
Being in the mind-reading business for so long i used the "applied conjecture"-method (also called "wild guessing procedure") and found out what you more or less probably want perhaps to be eventually done:

You have a file and filter some lines with a grep-statement. The filter matches several lines and you want only the first oneof these matches. (If this is not the case: don't bother telling us - we are all psychics.)

If so: you can either pipe that through the head-utility:

Code:
grep "<some-regex>" /path/to/file | head

or, more elegantly, use sed to do all in one:

Code:
sed -n '/<some-regex>/ {;p;q;}' /path/to/file

I hope this helps.

bakunin
# 5  
Old 01-13-2016
Could use awk too:
Code:
awk '/"dir Path"/ ;NR==1 {print $3}' forum.data

Where forum.data is the input file, containing the 4 lines from post1.

Hope this helps
# 6  
Old 01-13-2016
Quote:
Originally Posted by sea
Could use awk too:
Code:
awk '/"dir Path"/ ;NR==1 {print $3}' forum.data

Where forum.data is the input file, containing the 4 lines from post1.

Hope this helps
There aren't any double quotes in the input, and if the data we have been shown is the result of an earlier grep command, there is no reason to believe that the 1st line in the grep output was the 1st line in the original file. And, the request was for the entire line; not just the last field. So, if you want to do this with awk, shouldn't it just be:
Code:
awk '/dir Path:/' forum.data

# 7  
Old 01-13-2016
I did add the double quotes as i was getting a 2 line output without them (while writing)... 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

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

3. Shell Programming and Scripting

Count number of pattern matches per line for all files in directory

I have a directory of files, each with a variable (though small) number of lines. I would like to go through each line in each file, and print the: -file name -line number -number of matches to the pattern /comp/ for each line. Two example files: cat... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. Shell Programming and Scripting

[Solved] Finding the next line when a pattern matches

Hi I have a file like this Record 182: Rejected No Data found Record 196: Rejected File Not Found Record 202: Rejected Invalid argument Record 212: Rejected Bad data My requirement is to search for the value "Record" and if found, then return the next line of it. So,... (3 Replies)
Discussion started by: mr_manii
3 Replies

5. Shell Programming and Scripting

Remove if the above line matches pattern

but keep if does not I have a file: --> my.out foo: bar foo: moo blarg i am on vacation foo: goose foo: lucy foo: moose foo: stucky groover@monkey.org foo: bozo grimace@gonzo.net dear sir - blargo blargo foo: goon foo: sloppy foo: saudi gimme gimme gimme (3 Replies)
Discussion started by: spacegoose
3 Replies

6. Shell Programming and Scripting

get value that matches file name pattern

Hi I have files with names that contain the date in several formats as, YYYYMMDD, DD-MM-YY,DD.MM.YY or similar combinations. I know if a file fits in one pattern or other, but i donīt know how to extract the substring contained in the file that matches the pattern. For example, i know that ... (1 Reply)
Discussion started by: pjrm
1 Replies

7. Shell Programming and Scripting

Perl line count if it matches a pattern

#!/usr/bin/perl use Shell; open THEFILE, "C:\galileo_integration.txt" || die "Couldnt open the file!"; @wholeThing = <THEFILE>; close THEFILE; foreach $line (@wholeThing){ if ($line =~ m/\\0$/){ @nextThing = $line; if ($line =~ s/\\0/\\LATEST/g){ @otherThing =... (2 Replies)
Discussion started by: nmattam
2 Replies

8. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

9. Shell Programming and Scripting

Print line if first Field matches a pattern

Hi All, I would like my code to be able to print out the whole line if 1st field has a dot in the number. Sample input and expected output given below. My AWK code is below but it can;t work, can any expert help me ? Thanks in advance. {if ($1 ~ /*\.*/) { print $0 }} Input: ... (2 Replies)
Discussion started by: Raynon
2 Replies

10. Shell Programming and Scripting

Extract if pattern matches

Hi All, I have an input below. I tried to use the awk below but it seems that it ;s not working. Can anybody help ? My concept here is to find the 2nd field of the last occurrence of such pattern " ** XXX ccc ccc cc cc ccc 2007 " . In this case, the 2nd field is " XXX ". With this "XXX" term... (20 Replies)
Discussion started by: Raynon
20 Replies
Login or Register to Ask a Question