Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Piping commands using xargs

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 11-29-2017   -   Original Discussion by april
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 19 January 2018, 4:40 AM EST
Location: San Jose, CA, USA
Posts: 10,916
Thanks: 609
Thanked 3,817 Times in 3,261 Posts
Quote:
Originally Posted by april View Post
I tried both methods from Robin and they both worked. I see the file names and the grep results following each file name.

I tried Don's suggestion

Code:
echo "folder1/folder2 files in $targz..."

. That did not give me the file names. I only see the grep results.

Thank you all for your help.
Note that if you change the pattern you're looking for in the grep you also have to make a corresponding change to the first part of the string in the echo.
Sponsored Links
    #9  
Old Unix and Linux 12-08-2017   -   Original Discussion by april
april's Unix or Linux Image
april april is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 8 December 2017, 12:01 PM EST
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
I want to thank you all again for the replies. When I first read your comments about the standout output issues, I really didn't understand what you were talking about. Today I had to use the grep results and manipulate it with sed. I noticed after the sed, the file names were not lined up in the right place. I re-read your replies many times and did a lot of debugging. I finally understand!!!

Don't know if it's the best way to fix the problem but it does the job:


Code:
for targz in *.tar.gz
do
   echo "$targz" >&1
   tar -tf $targz | grep folder1/folder2>&1
done | sed ...

I wouldn't know to do any of these if you did not make the suggestions.
Thank you very much!!!
Sponsored Links
    #10  
Old Unix and Linux 12-08-2017   -   Original Discussion by april
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 January 2018, 4:32 AM EST
Location: Aachen, Germany
Posts: 11,954
Thanks: 353
Thanked 3,686 Times in 3,384 Posts
Glad you benefited from these fora, and hope you will in the future.

Two comments, though:
- the >&1 is pointless, as it means "duplicate fd1 (file descriptor) from fd1", and echo writes to fd1 anyhow.
- as sed has "grepping" capabilities by default, why don't you do everything needed in one sed command? If you need help on this, pls. post sample data and specific requirements.
    #11  
Old Unix and Linux 12-08-2017   -   Original Discussion by april
april's Unix or Linux Image
april april is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 8 December 2017, 12:01 PM EST
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
When I was started testing, the grep ... was put after the done. As a result, I only saw the grep results without the filenames so it appeared the filenames were "missing". I added the >&1 to make sure the filenames go to standad output because I didn't know why the names were "missing" at that time. I finally moved the |grep ... inside the loop and the names showed up in the output.

After reading your comments, I removed the >&1 and I still got the same output. Cool. Thank you.

Whoa, sed has grep capabilities??? Let me read about it first and if I still need help, I will post a new thread. Thank you!
Sponsored Links
    #12  
Old Unix and Linux 12-13-2017   -   Original Discussion by april
wbport's Unix or Linux Image
wbport wbport is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 16 January 2018, 6:14 PM EST
Location: Mississippi
Posts: 126
Thanks: 34
Thanked 18 Times in 18 Posts
Note that xargs reads a file one line at a time or it gets the equivalent data via a pipe. Its input file could be something you created/edited or from an ls or find command. The first argument of xargs is the command you want to run, other arguments are parameters (if any) of that command. Other arguments of your command are the list read into xargs.

For example, if myfile.txt contains:
file1
file2
file3

you could look for the word sample in all three files, ignoring case with the line


Code:
xargs <myfile.txt grep -i sample
   --- and what would be excuted is ---
grep -i sample file1 file2 file3

.

HTH
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Piping commands iliya24 UNIX for Dummies Questions & Answers 7 03-28-2013 11:24 PM
Piping multiple commands jrymer UNIX for Dummies Questions & Answers 3 08-06-2012 04:48 PM
piping problem with xargs ivpz Shell Programming and Scripting 7 08-09-2011 12:30 PM
Piping results of 'ls' to 'find' using 'xargs' MartynAbbott Solaris 4 08-21-2009 10:00 AM
use of xargs and prune piping with find command. venkatesht UNIX for Dummies Questions & Answers 1 01-28-2009 09:29 AM



All times are GMT -4. The time now is 05:43 AM.