LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 04-03-2014, 02:13 PM   #1
Automatic
Member
 
Registered: Mar 2013
Posts: 42

Rep: Reputation: Disabled
Only grep part of a line, but, output the full line?


So, I have program $a that shoves out a bunch of data, but, only the data before $z is what I want to grep for, anything after $z isn't important to the grep, but, is after the grep, so, what I need to do is something like:-

Code:
$a | sed "s/^(.*?)$z.*/\1/" | grep "$s" | $hereIsWhereIwantTheOriginallyDataBeforeISed'dItForTheGrepToNotSearchAfterThe$z.
is this possible?

For a more details reason, I need to search for all images that are 16:9 in a DIR, so, I do (This is a VirtualMachine, if you're wondering why I'm running as root):-
Code:
identify -format "%[fx:w/h] - %M\n" /root/folder/*
Which gives me:-
Code:
1.77778 - /root/folder/1.jpg
1.77778 - /root/5738554/2.jpg
1.62866 - /root/5738554/This file is on purposely stating the phrase 1.77778 to make it hard to just grep this.jpg
1.465 - /root/5738554/4.jpg
1.77778 - /root/5738554/5.jpg
1.77778 - /root/5738554/6.jpg
1.50037 - /root/5738554/7.jpg
1.77778 - /root/5738554/8.jpg
1.77778 - /root/5738554/9.jpg
1.6 - /root/5738554/10.jpg
1.61426 - /root/5738554/11.jpg
1.61426 - /root/5738554/12.jpg
1.77778 - /root/5738554/13.jpg
1.6 - /root/5738554/14.jpg
1.50038 - /root/5738554/15.jpg
1.49393 - /root/5738554/16.jpg
1.77778 - /root/5738554/17.jpg
1.78087 - /root/5738554/18.jpg
Passing that through sed:-
Code:
sed 's/\(.*\) - .*/\1/'
Gives me:-
Code:
1.77778
1.77778
1.62866
1.465
1.77778
1.77778
1.50037
1.77778
1.77778
1.6
1.61426
1.61426
1.77778
1.6
1.50038
1.49393
1.77778
1.78087
Now, that's much easier to grep, just a quick:-
grep 1.77778

But, now all I get is a huge list of 1.77778, what I really want is to get the original list back, but, only the lines that passed the grep without the file names (As the file names could include 1.77778)

So, how do I get the result before the sed with only the lines that passed the grep? Is this possible, or, do I have to create my own program to do this?

Last edited by Automatic; 04-03-2014 at 02:46 PM.
 
Old 04-03-2014, 02:44 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
why not just:

Code:
identify -format "%[fx:w/h] - %M\n" /root/folder/* | grep '^1.77778'

Last edited by estabroo; 04-03-2014 at 02:49 PM. Reason: code tags and clarified grep
 
Old 04-03-2014, 02:46 PM   #3
Automatic
Member
 
Registered: Mar 2013
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by estabroo View Post
why not just:

grep '^1.77778'
Because I was unware this was a thing, thanks, solves the issue!
 
  


Reply



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
[SOLVED] Remove new line in the output of grep command massy Programming 2 03-09-2014 01:57 AM
grep query to list 1 line [which is fixed] and the next line after it [variable text] Glenn D. Linux - Software 3 01-20-2011 06:21 AM
Want to print fouth line after resulted line using grep with tail command saurabhmehan Linux - Newbie 10 08-04-2010 11:38 PM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM
Grep's line numbers parsed into one line of output. judgex Programming 8 08-14-2006 04:22 AM

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

All times are GMT -5. The time now is 06:22 AM.

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
Open Source Consulting | Domain Registration