About Shell Tools : sort | cut | gzip | tar | tr | wc | fmt

1. Sort Your Output

Problem

You would like output in a stored order. but you don't want to write a custom sort function for your program or shell script.

Solution

Use the sort utility.You can sort one or more files by putting the file name on the command line:

         $ sort file1.txt file2.txt myotherfile.xyz

With no filenames on the command, sort will read from standard input so you can pipe the output from a previous command into sort:

$ somecommands | sort

There a few options to sort,but two of the three most worth remembering are :

  • sort -r (to reverse the order of the sort)
  • sort -f  (to fold lower- and uppercase characters together)
  • sort --ignore-case

2.How to sorting numbers?

Problem

When sorting  numeric data you notice that the order doesn't seem right

[root@DBAMAXWELL cp8]# sort somedata
2
200
21
250

Solution

You need to tell sort that the data should be sorted as numbers. specify a numeric sort with the -n option:
[root@DBAMAXWELL cp8]# sort -n somedata
2
21
200
250
[root@DBAMAXWELL cp8]#

sort -rn can be very handy in giving you a descending frequency list of something when combined with uniq -c.

[root@DBAMAXWELL cp8]# cut -d ":" -f7 /etc/passwd | sort | sort | uniq -c | sort -rn
     27 /sbin/nologin
      5 /bin/bash
      1 /sbin/shutdown
      1 /sbin/halt
      1 /bin/sync
      1 /bin/false
[root@DBAMAXWELL cp8]# 

3.How to sorting IP Addresses ?

[root@DBAMAXWELL cp8]# sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n ipaddr.list
10.0.0.2
10.0.0.5
10.0.0.20
192.168.0.2
192.168.0.4
192.168.0.12
[root@DBAMAXWELL cp8]#

4.How to cutting out parts of your output?

Problem

You need to look at only part of your fixed-width or column-based data. You'd like to take a subset of it ,based on the column position.

Solution

Use the cut command with the -c option to take particular columns 

[root@DBAMAXWELL cp8]# ps -l
F S   UID     PID    PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S     0   86528   86527  0  80   0 -  6886 -      pts/1    00:00:00 bash
4 S     0   86689   86528  0  80   0 - 34404 -      pts/1    00:00:00 su
4 S     0   87049   86690  0  80   0 - 34404 -      pts/1    00:00:00 su
4 S     0   87050   87049  0  80   0 -  6918 -      pts/1    00:00:00 bash
0 R     0   91869   87050  0  80   0 - 11372 -      pts/1    00:00:00 ps
[root@DBAMAXWELL cp8]# ps -l | cut -c12-15
   P
 865
 866
 870
 870
 918
 918
[root@DBAMAXWELL cp8]# ps -elf | cut -c58-
IME TTY          TIME CMD
r14 ?        00:00:55 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
r14 ?        00:00:00 [kthreadd]
r14 ?        00:00:00 [rcu_gp]
r14 ?        00:00:00 [rcu_par_gp]
r14 ?        00:00:00 [kworker/0:0H-events_highpri]
r14 ?        00:00:00 [mm_percpu_wq]
r14 ?        00:00:00 [rcu_tasks_rude_]
r14 ?        00:00:00 [rcu_tasks_trace]
r14 ?        00:00:07 [ksoftirqd/0]
r14 ?        00:01:31 [rcu_sched]
r14 ?        00:00:00 [migration/0]

5.How to removing duplicate lines?

Problem:

After selecting and/or sorting some data you notice that there are many duplicate lines in your results.You'd like to get ride of the duplicates,so that you can see just the unique value.

Solution:

 If you've just been sorting your output,add the -u option to the sort command:

  • $somesequence | sort -u

If you aren't running sort,just pip the output into uniq

  • somesequence > myfile
  • uniq myfile

6.How to compressing Files

Problem:

You need to compress some files and aren't sure of the best way to do it.

Solution:

$ tar cf tarball_name.tar directory_of_files

$ gzip tarball_name.tar

7.How to uncompressing Files

Problem

You need to uncompress one or more files ending in extension like tar,tar.gz,gz.tgz,Z,or zip

Solution

 7.Counting Lines,Words,or Characters in a File

Problem:

You need to know how many lines,words,or characters are in a given file

Solution:

[root@DBAMAXWELL cp8]# wc data_file
  8 132 754 data_file
[root@DBAMAXWELL cp8]# wc -l data_file 
8 data_file
[root@DBAMAXWELL cp8]# wc -w data_file
132 data_file
[root@DBAMAXWELL cp8]# wc -c data_file
754 data_file
[root@DBAMAXWELL cp8]# ls -l data_file
-rw-r--r-- 1 root root 754 Mar 17 18:38 data_file
[root@DBAMAXWELL cp8]# 

 8.How to Rewrapping Paragraphs

Problem:

You have some text with lines that are too long,or too short,so  you'd like to re-wrap them to be more readable.

Solution:

Use the fmt command, optionally with a goal and maximum line length.

[root@DBAMAXWELL cp8]# fmt mangled_text

[root@DBAMAXWELL cp8]# fmt 55 60 mangled_text

9.How to doing more with less

Problem:

You'd like to take better advantage of the features of the less paper.

Solution:

Read the less manage and use the $LESS variable with ~/.lessfilter and ~/.lesspipe files.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324131867&siteId=291194637