Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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
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
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.