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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
09-17-2007, 10:57 PM
|
#1
|
LQ Newbie
Registered: May 2007
Distribution: Gentoo
Posts: 18
Rep:
|
Problem with grep matching to end of line
Ok, I've been running linux for years and I feel like an idiot even asking this, It's probably going to be a duh answer,but anyways.
I diffed 2 files and grepped the output for just the new stuff and here was the output
Quote:
icedown@galaxy ~/data/text/stsw $ diff ls-lt ls-lt.old | grep '<'
< -rwxrwxrwx 1 gfs mtrim 2705 Sep 18 02:47 sn.0014.txt
< -rwxrwxrwx 1 gfs mtrim 2503 Sep 18 02:25 sn.0013.txt
< -rwxrwxrwx 1 gfs mtrim 2849 Sep 18 02:16 sn.0012.txt
< -rwxrwxrwx 1 gfs mtrim 1359 Sep 18 02:07 sn.0011.txt
< -rwxrwxrwx 1 gfs mtrim 1228 Sep 18 01:43 sn.0010.txt
< -rwxrwxrwx 1 gfs mtrim 1328 Sep 18 01:32 sn.0009.txt
< -rwxrwxrwx 1 gfs mtrim 1331 Sep 18 01:31 sn.0008.txt
< -rwxrwxrwx 1 gfs mtrim 2629 Sep 18 01:27 sn.0007.txt
< -rwxrwxrwx 1 gfs mtrim 1561 Sep 17 23:15 sn.0006.txt
< -rwxrwxrwx 1 gfs mtrim 1564 Sep 17 23:14 sn.0005.txt
< -rwxrwxrwx 1 gfs mtrim 1054 Sep 17 22:54 sn.0004.txt
< -rwxrwxrwx 1 gfs mtrim 1136 Sep 17 22:22 sn.0003.txt
< -rwxrwxrwx 1 gfs mtrim 1139 Sep 17 22:22 sn.0002.txt
< -rwxrwxrwx 1 gfs mtrim 1379 Sep 17 22:03 sn.0001.txt
< -rwxrwxrwx 1 gfs mtrim 1178 Sep 17 21:55 sn.0000.txt
< -rwxrwxrwx 1 gfs mtrim 1312 Sep 17 21:21 sn.0300.txt
< -rwxrwxrwx 1 gfs mtrim 1315 Sep 17 21:20 sn.0299.txt
< -rwxrwxrwx 1 gfs mtrim 1225 Sep 17 21:15 sn.0298.txt
< -rwxrwxrwx 1 gfs mtrim 1228 Sep 17 21:14 sn.0297.txt
< -rwxrwxrwx 1 gfs mtrim 1187 Sep 17 20:59 sn.0296.txt
< -rwxrwxrwx 1 gfs mtrim 1190 Sep 17 20:58 sn.0295.txt
|
I only want the filename part of the output so I made the command
diff ls-lt ls-lt.old | grep '<' | grep -o sn.0[0-3][0-9][0-9].txt
but nothing came out so I played with it for a little while and when i ran this
diff ls-lt ls-lt.old | grep '<' | grep -o sn.0[0-3][0-9][0-9].txt
i got the "sn.0???.tx" output, but i can't get that last "t" out of it? what am I doing wrong, I can just add it to the end of every line, that's not a problem, but I'm wondering what's going on here and how to deal with it in case i have something a little less constant in the future.
|
|
|
09-17-2007, 11:09 PM
|
#2
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,441
|
Looks like it should work - maybe try quotes on that as well.
|
|
|
09-17-2007, 11:21 PM
|
#3
|
LQ Newbie
Registered: May 2007
Distribution: Gentoo
Posts: 18
Original Poster
Rep:
|
Quotes did it. Don't know why I didn't try that... guess i can put my dunce hat on for the night
diff ls-lt ls-lt.old | grep '<' | grep -o 'sn.0[0-3][0-9][0-9].txt'
Thank you, I was about to pull my hair out. I am still curious as to why that is needed. Is there something special about the end of the line or could it be a bug?
|
|
|
09-18-2007, 12:50 AM
|
#4
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
You could use "cut" or "awk" to cut out the column you want.
diff ls-lt ls-lt.old | grep '<' | cut -d' ' -f5
diff ls-lt ls-lt.old | awk '/</{ print $5 }'
|
|
|
09-18-2007, 01:25 AM
|
#5
|
LQ Newbie
Registered: May 2007
Distribution: Gentoo
Posts: 18
Original Poster
Rep:
|
ok, i'll keep that in mind. I'm writing some scripts that are going to be dealing with more complex filenames and such. I was just curious if there was something that grep was seeing that I don't know about and may become an issue later with other programs like sed and others that use regular expressions
|
|
|
09-18-2007, 02:17 AM
|
#6
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,441
|
Must be a problem at your machine - I cut some of your data, and the first regex (no quotes) worked fine.
Last edited by syg00; 09-18-2007 at 02:18 AM.
Reason: less ambiguous
|
|
|
09-20-2007, 04:26 AM
|
#7
|
Member
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 249
Rep:
|
grep start at line
How do I start grep at line, say, 200?
(My output is too large and produces a segmentation fault, so i'm going to have to grep each 100 or so lines at a time.)
|
|
|
09-20-2007, 08:14 PM
|
#8
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
Maybe wrap it in a loop thus:
Code:
for rec in `cat yourfile`
do
grep stuff here $rec
done
which will process it rec by rec, slower, but file size should not be an issue.
|
|
|
09-20-2007, 09:24 PM
|
#9
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,441
|
Mmmmm - don't know about that; lots of blanks in there to trip over the default IFS.
It were me doing this, at about this point I'd be starting to think perl ....
|
|
|
09-20-2007, 10:38 PM
|
#10
|
Member
Registered: Oct 2006
Distribution: usually use arch
Posts: 103
Rep:
|
Quote:
Originally Posted by secretlydead
How do I start grep at line, say, 200?
(My output is too large and produces a segmentation fault, so i'm going to have to grep each 100 or so lines at a time.)
|
did you have the same problem with this awk version?
Code:
diff ls-lt ls-lt.old | awk '/</{ print $5 }'
it seems this could be exactly what you want. quick and simple way to do it.
|
|
|
09-21-2007, 02:45 AM
|
#11
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
You can set IFS to newline only easy enough.
Perl would be faster though if the file to process is that big, just seemed a bit excessive if he just wants the last field of each rec.
Also depends if this is just a one-off requirement or if its going to be run multiple times.
|
|
|
09-21-2007, 03:36 AM
|
#12
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
awk '/^</{for (i=1;i<=9;i++ ) $i=""}
{ gsub(" +","",$0) ; print}' "file"
|
|
|
09-23-2007, 05:43 AM
|
#13
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
If you want to use a range of lines, in sed you can start a sed command with a range like,
2000,2500 or use a regular expression like /\[General\]/,/$^/.
You can also use brackets to try to find a match within a range.
Also, using sed, you can add a quit command if you have found a match. That is commonly done to avoid reading all of the lines in a large file.
Code:
2000,2500{
/pattern/<command>
}
2501q
Using grep, look for the option to return a single result. That will abort after finding a match, and won't continue reading till the end of the file looking for another match.
The cut command would be ideal for cutting out a certain field. That is exactly what the command was written for. You could use head to select the top N lines and pipe the output to cut.
If you want to use a certain range of lines, you can use sed to filter through only those lines and pipe the output to the cut command:
Code:
diff ls-lt ls-lt.old | sed -n "${start},${end}p;$((${end}+1))q | grep '<' | cut -d' ' -f5
Last edited by jschiwal; 09-23-2007 at 05:53 AM.
|
|
|
All times are GMT -5. The time now is 08:10 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|