XARGS Madness

Go down

XARGS Madness

Post by Nazneennazim on Fri Mar 12, 2010 2:22 pm

Have been trying to understand the working of xargs for quite sometime now.Can someone help me understand and give me some sample code that will help me practise.

The following is what I have tried but could not get the output
find <directory name> - name "*.txt" -print | xargs grep <pattern> | tee <file_name>

Nazneennazim

Posts : 10
Points : 16
Join date : 2010-03-01

View user profile

Back to top Go down

Re: XARGS Madness

Post by akalya on Fri Mar 12, 2010 3:34 pm

this is a simple program
the output of the command preceding is passed line by line to the command following xargs
[335802@oracleclient ~]$ cat>datadata
data1
data2

[1]+ Stopped cat > datadata
[335802@oracleclient ~]$ cat>data1
data1
hello1

[2]+ Stopped cat > data1
[335802@oracleclient ~]$ cat>data2
data2
hello2

[3]+ Stopped cat > data2
[335802@oracleclient ~]$ cat datadata | xargs cat
data1
hello1

data2
hello2

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: XARGS Madness

Post by Christopher on Fri Mar 12, 2010 3:38 pm

For example, take this example
Code:
ls *.txt | xargs cat
ls *.txt ll return all files with txt extension. each filename is given as input to cat because of xargs. (this command displays contents of all the txt files)
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: XARGS Madness

Post by jeeva on Fri Mar 12, 2010 3:41 pm

find <directory name> - name "*.txt" -print

the above line lists all the .txt files in that particular directory.

| symbol passes the output of the above code to the command on the right

xargs grep <pattern>

in this xargs does the job of taking in multiple arguements(which is actually a list) and performs the grep function

jeeva

Posts : 50
Points : 93
Join date : 2010-03-04

View user profile

Back to top Go down

Re: XARGS Madness

Post by akalya on Fri Mar 12, 2010 3:46 pm

[335802@oracleclient ~]$ cat>y.txt
12345
34567

[5]+ Stopped cat > y.txt
[335802@oracleclient ~]$ cat>y1.txt
123
abc
def

[6]+ Stopped cat > y1.txt
[335802@oracleclient ~]$ cat>y12.txt
andfff
dfree

[7]+ Stopped cat > y12.txt
[335802@oracleclient ~]$ find ./ -name "y*" -type f -print | xargs grep "123tee hg.txt
./y.txt:12345
./y1.txt:123
[335802@oracleclient ~]$ find ./ -name "y*" -type f -print | xargs grep "a" e hg.txt
./y12.txt:andfff
./y1.txt:abc

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: XARGS Madness

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum