Wednesday, July 9, 2014

Unix Shell: xargs

1. xargs with nothing
By default it will use /bin/echo to form command:
terminal:
After typing xargs, system let us to type in the string. So we typed string "Hello world", then enter the newline. At this time, we enter Ctrl+d to send the "end" signal. Then xargs will form the command: /bin/echo Hello world! and output another line of "Hello world!" in screen.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ xargs  
 Hello world!  
 Hello world!  

2. xargs with -t option
-t option will make xargs to print out the formed command before executing.
terminal:
After typing xargs -t, system let us to type in the string. So we typed string "Hello world", then enter the newline. At this time, we enter Ctrl+d to send the "end" signal. Then xargs will form the command: /bin/echo Hello world!
And -t option will make it output this formed command, then print out the execution result.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ xargs -t  
 Hello world!  
 /bin/echo Hello world!   
 Hello world!  

3. xargs with -n option
-n option will make xargs to print out specified items in one line. The standard to specify the number of items is "delimiter" which is specified by -d option.
text:
 Hello world! Hello New York!  

terminal:
1) First command specify we can only output 2 items per line.
2) Second command specify we can only output 1 item per line.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ xargs -n 2 < text  
 Hello world!  
 Hello New  
 York!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ xargs -n 1 < text  
 Hello  
 world!  
 Hello  
 New  
 York!  

4. xargs with -d option
-d option will make xargs specify the delimiter of the input and output.

text:
 Hello world!:Hello New York!  

terminal:
1) With default delimiter, it just take the entire line as the input of the command
2) With delimiter colon, it will ignore the delimiter itself and read in the remaining part.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ xargs -t <text  
 /bin/echo Hello world!:Hello New York!   
 Hello world!:Hello New York!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ xargs -d: -t <text  
 /bin/echo Hello world! Hello New York!   
 Hello world! Hello New York!  

terminal:
1) With colon as the delimiter, and we specify that one line could only contain at most one item. In this case, "Hello world!" will be taken as one item and "Hello New York!" will be taken as one item.
2) With default space as the delimiter, and we specify that one line could only contain at most one item. In this case, "Hello" will be taken as one item, "world!:Hello" will be taken as one item, "New" will be taken as one item, and finally "York" will be taken as one item.
-t option will make xargs output all command itself before executing it.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ xargs -d: -t -n 1 < text  
 /bin/echo Hello world!   
 Hello world!  
 /bin/echo Hello New York!   
 Hello New York!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ xargs -t -n 1 < text  
 /bin/echo Hello   
 Hello  
 /bin/echo world!:Hello   
 world!:Hello  
 /bin/echo New   
 New  
 /bin/echo York!   
 York!  

5. xargs with -p option
-p option will make xargs prompt waiting for the user's confirmation.
text:
 Hello world!:Hello New York!  


terminal:
After entering the xargs command, system asked us if we want it to run the given command, in this case, we enter "y". And the command get run.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ xargs -p <text  
 /bin/echo Hello world!:Hello New York! ?...y  
 Hello world!:Hello New York!  

6. xargs with find, and grep
terminal:
1 - 3) Create 3 files containing 3 strings
4) List all files whose name starting with o
5) grep "Hello" string from 4 places: /dev/null o1 o2 and o3
6) Combine the "find" command and "grep" command with xargs.
It will form the command: grep Hello /dev/null o1 o2 o3.
o1 o2 and o3 is the result of find command, /dev/null is used to defend one situation: what if find command doesn't reply anything, in this case, it will prompt user to input one source file.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello New York!" > o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello Chicago!" > o2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello Boston!" > o3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find o[1-3]  
 o1  
 o2  
 o3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ grep "Hello" /dev/null o1 o2 o3  
 o1:Hello New York!  
 o2:Hello Chicago!  
 o3:Hello Boston!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find o[1-3] | xargs -t grep "Hello" /dev/null  
 grep Hello /dev/null o1 o2 o3   
 o1:Hello New York!  
 o2:Hello Chicago!  
 o3:Hello Boston!  

No comments:

Post a Comment