Xargs, sort, uniq command

The xargs, sort, and uniq commands are introduced by a question from LeetCode and used to understand;

The title is this: write a bash script to count a text file words.txt.

The content of words.txt is:

the day is sunny the the

the sunny
is is

div>

1.cat words.txt | sort to see what the effect will be

[[emailprotected] tmp]# cat words.txt | sort

the day
is sunny the the
the sunny
is is

div>

The sort command will arrange the first column of the text file in ASCII code order by default, and output the result to standard output.

2. Use cat words.txt | xargs -n1 | sort | uniq -c to see what the effect is

[[ email protected] tmp]# cat words.txt | xargs -n1 | sort | uniq -c

1 day
3 is
2 sunny
4 the

uniq The command can only perform deduplication operations on adjacent lines, so before deduplication, the text lines must be sorted to make the duplicate lines together. This is the reason why sort first; -c is the number of statistics.

3. Use cat words.txt | xargs -n1 | sort | uniq -c | sort -nr to see the effect

 [[email protected] tmp]# cat words.txt | xargs -n1 | sort | uniq -c | sort -n

1 day
2 sunny
3 is
4 the
[[email protected]
-n93yom tmp]# cat words.txt | xargs -n1 | sort | uniq -c | sort -nr
4 the
3 is
2 sunny
1 day

sort -n is sorted by number, the default is ascending order, -r is descending order, so sort -nr is sorted by number in descending order

4. Combine the awk command to output the result. :

cat words.txt | xargs -n1 | sort | uniq -c | sort -nr | awk'{print $2″ “$1}’

< pre>[[email protected] tmp]# cat words.txt | xargs -n1 | sort | uniq -c | sort -nr | awk {print $2″ “$1}

the
4

is 3

sunny
2

day
1

The xargs command is introduced below:

1. cat words.txt | xargs converts the text into one line to output

[[emailprotected] tmp]# cat words.txt |  xargs

the day
is sunny the the the sunny is < span style="color: #0000ff;">is

Add -n number to convert the text into multiple lines of output, the number is the number of words in each line

[[emailprotected] tmp]# cat words.txt | xargs -n1

the
day
is
sunny
the
the
the
sunny
is
is

xargs -dx specify the separator for output

[[email protected] tmp]# echo "nameXnameXnameXname" | xargs -dX

name name name name

Define a script sk.sh

#!/bin/bash

#sk.sh command content, print out all parameters.

echo $
*

Then execute the command: cat words.txt | xargs -I {} ./sk.sh -p {} -l

An option -I of xargs, use -I to specify a replacement string {}, this string will be replaced when xargs is expanded, when -I is used in combination with xargs, each parameter command will be executed once :

[[email protected] tmp]# cat words.txt | xargs -I {} ./sk.sh -p {} -l

-p the day is sunny the the -l
-p the sunny is is -l pre>

Copy all the .sh files in the current folder to the tmp file

ls *.sh | xargs -n1 -I {} cp {} /root/tmp /

[[email protected] ~]# ll

total
20-rw-r--r-- 1 span> root root 49 Aug 15 17:01 mysql.sh-rw-r--r - 1 root root 640 Aug 15 17:13 upgrade.sh
[[email protected]
-n93yom ~]# ls *.sh | xargs -n1 -I {} cp {} /root/tmp/
[[email protected]
-n93yom ~]# cd tmp/
[[email protected]
-n93yom tmp]# ll
total
20
-rw-r--r-- 1 root root 49 Aug 22 22:53 span> mysql.sh
-rw-r--r-- 1 root root 1559< /span> Aug 19 23:13 passwd
-rwxr-xr-x 1 root root 21 Aug 22 22:44< /span> sk.sh
-rw-r--r-- 1 root root 640< /span> Aug 22 22:53 upgrade.sh
-rw-r--r-- 1 root root 41< /span> Aug 22 14:03 words.txt

xargs combined with find

Use rm to delete too much You may get an error message: /bin/rm Argument list too long. Use xargs to avoid this problem:


find. -type f -name "*.log" -print0 | xargs -0 rm -f xargs -0 Use as the delimiter. Count the number of lines of all php files in a source code directory: find. -Type f -name "*.php" -print0 | xargs -0 wc -l Find all jpg files and compress them: find.- type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz xargs Other applications If you have a file that contains many URLs you want to download, you can use xargs to download all the links: # cat url-list.txt | xargs wget -c

Reference: https:/ /www.runoob.com/linux/linux-comm-xargs.html

the day is< span style="color: #000000;"> sunny the the

the sunny is is

p>

[[emailprotected] tmp]# cat words.txt | sort

the day
is sunny the the
the sunny
is is

p>

[[emailprotected] tmp]# cat words.txt | xargs -n1 | sort | uniq -c

1 day
3 is
2 sunny
4 the

[[emailprotected] tmp]# cat words .txt | xargs -n1 | sort | uniq -c | sort -n

1 day
2 sunny
3 is
4 the
[[email protected]
-n93yom tmp]# cat words.txt | xargs -n1 | sort | uniq -c | sort -nr
4 the
3 is
2 sunny
1 day

[[emailprotected] tmp]# cat words .txt | xargs -n1 | sort | uniq -c | sort -nr | awk '{print $2 ""$1}'

the
4
is 3
sunny
2
day
1

[[emailprotected] tmp]# cat words .txt | xargs

the day
is sunny the the the sunny is < span style="color: #0000ff;">is

[[emailprotected] tmp]# cat words.txt | xargs -n1

the
day
is
sunny
the
the
the
sunny
is
is

[[emailprotected] tmp]# echo "nameXnameXnameXname" | xargs -dX

name name name name

#!/bin/bash

#sk.sh command content, print out all parameters.

echo $
*

[[email protected] tmp]# cat words.txt | xargs -I {} ./sk.sh -p {} -l

-p the day is sunny the the -l
-p the sunny is is -l pre>

[[email protected] ~]# ll

total
20-rw-r--r-- 1 span> root root 49 Aug 15 17:01 mysql.sh-rw-r--r - 1 root root 640 Aug 15 17:13 upgrade.sh
[[email protected]
-n93yom ~]# ls *.sh | xargs -n1 -I {} cp {} /root/tmp/
[[email protected]
-n93yom ~]# cd tmp/
[[email protected]
-n93yom tmp]# ll
total
20
-rw-r--r-- 1 root root 49 Aug 22 22:53 span> mysql.sh
-rw-r--r-- 1 root root 1559< /span> Aug 19 23:13 passwd
-rwxr-xr-x 1 root root 21 Aug 22 22:44< /span> sk.sh
-rw-r--r-- 1 root root 640< /span> Aug 22 22:53 upgrade.sh
-rw-r--r-- 1 root root 41< /span> Aug 22 14:03 words.txt

Leave a Comment

Your email address will not be published.