Shell command - wc, tr for file and content processing

Posted by xydon1 on Tue, 07 May 2019 22:40:02 +0200

File and Content Processing - wc, tr

1. wc: Count lines, words, or bytes of a file

Functional description of wc command

The wc command calculates the number of words.With the wc command, we can calculate the number of bytes, words, or columns of a file. If you do not specify a file name or give it a file name of'-', the wc command reads data from the standard input device.

Syntax format of wc command

wc [OPTION]... [FILE]...
wc [-clw] [ - help] [ - version] [file...]

Option description for wc command

The wc option is rarely used. Table 1 shows the parameters and explanations of the wc command.

Table 1: Parameters and description of wc command

Parameter Options interpretative statement
-c or--bytes or--chars Show only the number of Bytes.
-l or--lines Show only the number of rows.
-w or--words Show only the number of words.
--help Online help.
--version Display version information.

Practical operation of wc command

Example 1: By default, wc calculates the number of lines, words, and bytes of the specified file using the following commands:

--------------------------------------------------------------
==>The command is as follows <==
--------------------------------------------------------------
wc testfile

--------------------------------------------------------------
==>View the contents of the testfile file <==
--------------------------------------------------------------
[root@nfs01 /data]# cat testfile
 Linux networks are becoming more common, but simplification is often overlooked  
Question.Unfortunately, in today's environment, all networks are potential *** targets.  
From secret military research networks to small home LAN.  
Linux Network Securty focuses on protecting Linux in a network environment, where  
The security of the entire network needs to be considered, not just isolated machines.  
It uses a combination of theoretical and practical techniques to teach administrators how to install and  
Use secure applications and how they work and why they are necessary.

--------------------------------------------------------------
==>Using wc statistics, the resu lt s are as follows <==
--------------------------------------------------------------
[root@nfs01 /data]# wc testfile
  7   9 530 testfile

The three numbers represent the number of lines, words, and bytes in the testfile.If you want to count information from multiple files at the same time, for example, testfile_1, testfile_2 for testfile, you can use the following commands:

--------------------------------------------------------------
==>Statistical information for three files <==
--------------------------------------------------------------
wc testfile testfile_1 testfile_2

--------------------------------------------------------------
==>The output is as follows<==
--------------------------------------------------------------
wc testfile testfile_1 testfile_2#Statistical information for three files  
3 92 598 testfile#The first file has 3 lines, 92 words, 598 bytes  
9 18 78 testfile_1#The second file has 9 lines, 18 words, 78 bytes  
3 6 32 testfile_2#The third file has 3 lines, 6 words, 32 bytes  
15 116 708 Total dosage#The three files total 15 rows, 116 words, 708 bytes

Example 2: See how many users/etc/passwd has

--------------------------------------------------------------
==>The command is as follows<==
--------------------------------------------------------------
cat /etc/passwd
cat /etc/passwd|wc -l
cat /etc/passwd|wc -L

--------------------------------------------------------------
==>Source File Content<==
--------------------------------------------------------------
root@nfs01 ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
oldboy:x:1000:1000::/home/oldboy:/bin/bash
ntp:x:38:38::/etc/ntp:/sbin/nologin

--------------------------------------------------------------
==>Count rows<==
--------------------------------------------------------------
[root@nfs01 ~]# cat /etc/passwd|wc -l
22

--------------------------------------------------------------
==>Longest Statistics<==
--------------------------------------------------------------
[root@nfs01 ~]# cat /etc/passwd|wc -L
99

2. tr: Replace or delete characters

Functional description of tr command

The tr command replaces, reduces, or deletes characters from standard input and writes the results to standard output, which is case-sensitive in Linux.Use double quotation marks for all characters.

Syntax format of tr command

Tr [-cdst] [- help] [- version] [first character set] [second character set]
tr [Options]... SET1 [SET2]

Option description for tr command

The tr option is not used much. Table 1 shows the parameters and explanations of the tr command, and Table 2 shows the range of the TR character set.

Table 1: Parameters and explanation of the tr command

Parameter Options interpretative statement
-c,--complement Deselect the setting character.That is, the parts conforming to SET1 are not processed, and the rest of the non-conforming parts are converted.
-d, - delete Delete instruction character
-s, - squeeze-repeats Reduces the number of consecutive repeated characters to a specified single character
-t, - struncate-set1 Reduce the SET1 range to equal the SET2 set length
--help Display program usage information
--version Displays version information for the program itself

Table 2: Explanation of the range of the tr character set

Parameter Options interpretative statement
\ NNN The character NNN of the octal value (characters from 1 to 3 octal values)
\|Backslash
\ a Ctrl-G ringtone
\ b Ctrl-H Backspace
\ f Ctrl-L Page Break
\ n Ctrl-J New Line
\ r \ n Ctrl-M Enter
\ t Ctrl-I tab key
\ v Ctrl-X Horizontal Tab
CHAR1-CHAR2 Character ranges are specified from CHAR1 to CHAR2. Range designations are based on the order of ASCII codes and can only range from small to large, not from large to small.
[CHAR *] This is a SET2 specific setting that repeats the specified character until it is the same length as SET1
[CHAR * REPEAT] This is also a SET2-specific setting that repeats the specified characters until the set number of REPEATs (REPEAT's number is calculated in 8 digits, starting with 0)
[: alnum: ] All alphabetic characters and numbers
[: alpha: ] All alphabetic characters
[: blank: ] All horizontal spaces
[: cntrl: ] All control characters
[: digit: ] All Numbers
[: graph: ] All printable characters (excluding space characters)
[: lower: ] All lowercase letters
[: print: ] All printable characters (including space characters)
[: punct: ] All punctuation characters
[: space: ] All horizontal and vertical space characters
[: upper: ] All uppercase letters
[: xdigit: ] All 16-digit numbers
[= CHAR =] All characters that match the specification (CHAR in the equal sign, for characters you can customize)

Practice of tr command

The practice text is as follows:

--------------------------------------------------------------
==>The command is as follows<==
--------------------------------------------------------------
cat oldboy.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448

not 4900000448
my god ,i am not oldbey,but OLDBOY!

--------------------------------------------------------------
==>The demo is as follows<==
--------------------------------------------------------------
[root@oldboyedu  ~]# cat oldboy.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448

not 4900000448
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu  ~]#

Example 1: Replace the abc that appears in the file with xyz.

[root@oldboyedu  ~]# tr 'abc' 'xyz' < oldboy.txt     
                            #The tr command connection file is special and requires a redirection symbol'<'             
I xm oldyoy texzher!
I texzh linux.

I like yxdminton yxll,yillixrd yxll xnd zhinese zhess!
my ylog is http://oldyoy.ylog.51zto.zom
our site is http://www.etixntixn.org
my qq num is 49000448

not 4900000448
my god ,i xm not oldyey,yut OLDBOY!
[root@oldboyedu  ~]#

Description: Where A is converted to x, b to y, c to z, instead of just replacing the string a b c with the string X Y Z

Example 2: Use the tr command to case letters.

[root@oldboyedu  ~]# tr '[a-z]' '[A-Z]' <oldboy.txt
I AM OLDBOY TEACHER!
I TEACH LINUX.

I LIKE BADMINTON BALL,BILLIARD BALL AND CHINESE CHESS!
MY BLOG IS HTTP://OLDBOY.BLOG.51CTO.COM
OUR SITE IS HTTP://WWW.ETIANTIAN.ORG
MY QQ NUM IS 49000448

NOT 4900000448
MY GOD ,I AM NOT OLDBEY,BUT OLDBOY!
[root@oldboyedu  ~]#

Example 3: Replace the numbers 0-9 with a-j.(also a one-to-one correspondence)

[root@oldboyedu  ~]# tr '[0-9]' '[a-j]' <oldboy.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.fbcto.com
our site is http://www.etiantian.org
my qq num is ejaaaeei

not ejaaaaaeei
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu  ~]#

Example 4: Delete each character in oldboy from the file.

[root@oldboyedu  ~]# tr -d 'oldboy' <oldboy.txt
I am  teacher!
I teach inux.

I ike amintn a,iiar a an chinese chess!
m g is http://.g.51ct.cm
ur site is http://www.etiantian.rg
m qq num is 49000448

nt 4900000448
m g ,i am nt e,ut OLDBOY!
[root@oldboyedu  ~]#

Note: Any o, l, d, b, y characters that appear in the file will be deleted, not just the o l d b o y string.

As of today, if you have any questions or errors, you are always welcome to comment on them.

Topics: Linux network ftp DBus