Home Man
Today's Posts

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

Login to Reply

Thread Tools Search this Thread
# 8  
Old 11-29-2017
Originally Posted by april
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
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.
# 9  
Old 12-08-2017
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:
for targz in *.tar.gz
   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!!!
# 10  
Old 12-08-2017
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 12-08-2017
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!
# 12  
Old 12-13-2017
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:

you could look for the word sample in all three files, ignoring case with the line
xargs <myfile.txt grep -i sample
   --- and what would be excuted is ---
grep -i sample file1 file2 file3


Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Proper chaining and piping of commands SkySmart Shell Programming and Scripting 2 01-08-2016 01:46 AM
Xargs: multiple commands to each argument urello Shell Programming and Scripting 1 03-02-2015 12:25 AM
Piping commands iliya24 UNIX for Dummies Questions & Answers 7 03-28-2013 10:24 PM
Piping multiple commands jrymer UNIX for Dummies Questions & Answers 3 08-06-2012 03:48 PM
piping problem with xargs ivpz Shell Programming and Scripting 7 08-09-2011 11:30 AM
Piping results of 'ls' to 'find' using 'xargs' MartynAbbott Solaris 4 08-21-2009 09:00 AM
Piping through commands read as variables from file fitzwilliam Shell Programming and Scripting 1 07-20-2009 08:19 AM
use of xargs and prune piping with find command. venkatesht UNIX for Dummies Questions & Answers 1 01-28-2009 08:29 AM
Can Xargs execute multiple commands of evry input file nilesrex Shell Programming and Scripting 4 08-30-2006 08:39 AM
Executing commands with xargs bmopal UNIX for Dummies Questions & Answers 1 08-13-2001 04:05 PM

All times are GMT -4. The time now is 08:50 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
Show Password