LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-04-2014, 06:32 PM   #1
ani78
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Rep: Reputation: Disabled
find all matching lines before first matc


I am trying to print all the lines from a file before the first match. I have the same entries again in the file, but I don't need that lines.
Tried awk "{print} /${pattern}/ {exit}" and sed "/$pattern/q" (my serach is based on a variable). But both these commands are printing all the line before the last match
ex: my file is like
abc
bcd
def
xyz
def
lmno
def
xvd

when my pattern is 'def', i just need abc and bcd . but the above commands are printing, all the lines before the last 'def'. could you please provide some idea
 
Old 11-04-2014, 07:01 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
I am not sure I follow the issue as using your too codes I get exactly the same output and only up to the first occurrence:
Code:
$ awk '{print}/def/{exit}' file
abc 
bcd 
def 
$ sed '/def/q' file
abc 
bcd 
def
Now to nme this is not quite the brief as it also prints the pattern being searched for and your example seemed to imply this was not desired.
However, as you can see I do not print any further than the first occurrence.
 
Old 11-04-2014, 07:12 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,721

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120
But the OP wants to use a bash (?) variable. The same logic error applies regardless.
The posted sed should work - maybe we need to know more about sed and Linux version.

AFAIK awk needs the variable assigned with -v before it is available for processing (grail, is this true ?). But again the posted code is remiss.
 
Old 11-05-2014, 12:20 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Yes awk is not fond of double quotes and it makes life more complicated if you actually need to start writing strings in your code.

So you can either use -vpat="$pattern" as suggested or after last quote and prior to calling files you could do pat="$pattern":
Code:
awk '{print}$0 ~ pat{exit}' pat="$pattern" file
 
Old 11-05-2014, 04:27 PM   #5
ani78
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks Grail,

Interesting... when I tried to execute the same command, its giving all the entries upto the last match

I am trying to get the user access details for the last 10 days using the output of the "last" command. If I try to find the access until Oct 25 (2014), its showing all entries until Oct 25 (2013). As "last" is not showing the year, I can't provide that information.

I am using teh command "perl -le 'print scalar localtime (time() - 240*60*60);' |awk '{print $2 ,$3 }'" to find the month and date, assign that to a variable and then using sed/awk to get the details
 
Old 11-05-2014, 04:39 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,508

Rep: Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102Reputation: 2102
Here it is with grep

Code:
nlines=$(cat $file | wc -l)
grep -B $nlines -m 1 $pattern $file
nlines gets the number of lines in the file
-B $nlines tells grep to print that many lines before each match (setting this to nlines means it can print up to the entire file, if the only match is on the last line)
-m 1 tells grep to only return one (the first) match
 
Old 11-05-2014, 05:47 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,462

Rep: Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517Reputation: 1517
Here is a solution with sed:
Code:
sed -n "/$pattern/q;p"
Suppress the automatic printing, quit if there is a match, then an explicit print of any line that did not match. Note that the sed manpage does say that you have to suppress the automatic print to avoid printing the line that triggered the "q" command.

Like the others who have posted, I cannot duplicate the behavior of continuing until the last pattern match. Perhaps the output from your last command has some non-printing characters or whitespace differences that are causing the first match to fail.
 
Old 11-05-2014, 06:57 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
So I am curious now, with the additional information, why you do not simply perform all the tasks in Perl? It can easily perform all the tasks of sed/awk/grep and you have already stepped into it to perform a trivial
task.
 
Old 11-06-2014, 04:38 PM   #9
ani78
LQ Newbie
 
Registered: Nov 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks everyone

Unfortunately I am not at all good in perl

Looks like the issue was with the format of the date format I am extracting using the perl command is not matching with 'last' command date format. Replaced all the spaces with a single # and seems its working now

Thanks again
 
Old 11-06-2014, 06:42 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Glad you finally have a solution. Please remember to mark question as SOLVED
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 05:30 PM
[SOLVED] bash matching two files lines by lines rperezalejo Programming 6 01-12-2012 06:42 AM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
matching array's lines?? HyperTrey Programming 7 05-29-2008 07:42 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration