Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

2.6 Text file editing command


May 23, 2021 That's what Linux should learn



Through the previous sections of the study, the reader should basically master the switching of working directories and the management of files. " Everything is a file" in a Linux system, and configuring a service program is naturally the configuration of the editor. I f you can't expertly consult the configuration files of your system or service, you'll be really embarrassed when you work later. T his section explains a few commands for viewing the contents of a text file. As for the complexity of using the editor, it will be explained in Chapter 4 with the Shell script content.

1. cat command

The cat command is used to view plain text files (with less content) in the format "cat (options) . . .

There are several commands in the Linux system that are used to view text content, each with its own characteristics, such as this cat command, which is used to view plain text files with less content. Cat this command is also very good to remember, because cat in English means "cat", kittens give you a petite, lovely feeling?

If you want to show the line number by the way when viewing the text content, you might want to add a -n parameter after the cat command:

[root@linuxprobe ~]# cat -n initial-setup-ks.cfg 1 #version=RHEL7 2 # X Window System configuration information 3 xconfig --startxonboot 4
5 # License agreement 6 eula --agreed 7 # System authorization information 8 auth --enableshadow --passalgo=sha512 9 # Use CDROM installation media 10 cdrom 11 # Run the Setup Agent on first boot 12 firstboot --enable 13 # Keyboard layouts 14 keyboard --vckeymap=us --xlayouts='us' 15 # System language 16 lang en_US. U TF-8 .................. Omit some of the output information...

Cat command details: https://www.linuxcool.com/cat

2. More command

The more command is used to view plain text files (with more content) in the format of "more (options) files".

If you need to read a novel or a very long profile, "kittens" really don't fit. B ecause once you use the cat command to read long text content, the information will quickly roll over on the screen, resulting in their own time to see, the content has been turned over. T herefore, for long text content, it is recommended to use the more command to view. T he more command uses a percentage at the bottom to prompt you how much you've read. You can also turn the page down using the space bar or enter key:

[root@linuxprobe ~]# more initial-setup-ks.cfg

version=RHEL7

  1. # X Window System configuration information
  2. xconfig --startxonboot
  3. # License agreement
  4. eula --agreed
  5. # System authorization information
  6. auth --enableshadow --passalgo=sha512
  7. # Use CDROM installation media
  8. cdrom
  9. # Run the Setup Agent on first boot
  10. firstboot --enable
  11. # Keyboard layouts
  12. keyboard --vckeymap=us --xlayouts='us'
  13. # System language
  14. lang en_US.UTF-8
  15. ignoredisk --only-use=sda
  16. # Network information
  17. network --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
  18. network --bootproto=dhcp --hostname=linuxprobe.com
  19. --More--(43%)

3. Head command

The head command is used to view the first N lines of a plain text document in the format of "head ( options ) . . .

When reading text, it's hard to guarantee that the entire file will be read from start to finish. W hat if you only want to see the first 20 lines of text? Head commands can be useful:

[root@linuxprobe ~]# head -n 20 initial-setup-ks.cfg

version=RHEL7

  1. # X Window System configuration information
  2. xconfig --startxonboot
  3. # License agreement
  4. eula --agreed
  5. # System authorization information
  6. auth --enableshadow --passalgo=sha512
  7. # Use CDROM installation media
  8. cdrom
  9. # Run the Setup Agent on first boot
  10. firstboot --enable
  11. # Keyboard layouts
  12. keyboard --vckeymap=us --xlayouts='us'
  13. # System language
  14. lang en_US.UTF-8
  15. ignoredisk --only-use=sda
  16. # Network information
  17. network --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
  18. [root@linuxprobe ~]#

4. tail command

The tail command is used to view the back N lines of a plain text document or to continuously refresh the content in the format "tail (options) ( files) " .

We may also encounter another situation, such as the need to look at the last 20 lines of text content, when you need to use the tail command. T he tail command operates very similarly to the head command, and only the "tail -n 20 file name" command is required to achieve this effect. The most powerful feature of the tail command is the ability to continuously refresh the contents of a file, which is especially useful when you want to see the latest log files in real time, when the command format is "tail -f file name":

[root@linuxprobe ~]# tail -f /var/log/messages May 4 07:56:38 localhost gnome-session: Window manager warning: Log level 16: STACK_OP_ADD: window 0x1e00001 already in stack May 4 07:56:38 localhost gnome-session: Window manager warning: Log level 16: STACK_OP_ADD: window 0x1e00001 already in stack May 4 07:56:38 localhost vmusr[12982]: [ warning] [Gtk] gtk_disable_setlocale() must be called before gtk_init() May 4 07:56:50 localhost systemd-logind: Removed session c1. Aug 1 01:05:31 localhost systemd: Time has been changed Aug 1 01:05:31 localhost systemd: Started LSB: Bring up/down networking. AUG 1 01:08:56 localhost dbus-daem: dbus [1124]: [system] Activating service name = 'com.redhat.subscriptionManager' (use servicehelper) AUG 1 01:08:56 localhost dbus [1124]: [1124]: [SYSTEM] Activating service name = 'com.redhat.SubscriptionManager' (using servicehelper) Aug 1 01:08:57 localhost dbus-daemon: dbus [1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager' Aug 1 01:08:57 localhost dbus [1124]: "SuccessFully ActiVated Service 'com.redhat.subscriptionManager' 5.Tr command

The tr command is used to replace characters in a text file in the format of "tr (original characters) and "target characters".

In many cases, we want to quickly replace some words in the text, or replace the entire text content, if manual replacement, it is inevitable that the workload is too heavy, especially when the need to deal with large quantities of content, manual replacement is not realistic. A t this point, you can read the text to be processed using the cat command, and then pass the text content to the tr command for replacement through the pipeline character (see Chapter 3). For example, replace all English in a text content with capitals:

[root@linuxprobe ~]# cat anaconda-ks.cfg | tr [a-z] [A-Z]

VERSION=RHEL7

  1. # SYSTEM AUTHORIZATION INFORMATION
  2. AUTH --ENABLESHADOW --PASSALGO=SHA512
  3. # USE CDROM INSTALLATION MEDIA
  4. CDROM
  5. # RUN THE SETUP AGENT ON FIRST BOOT
  6. FIRSTBOOT --ENABLE
  7. IGNOREDISK --ONLY-USE=SDA
  8. # KEYBOARD LAYOUTS
  9. KEYBOARD --VCKEYMAP=US --XLAYOUTS='US'
  10. # SYSTEM LANGUAGE
  11. LANG EN_US.UTF-8
  12. # NETWORK INFORMATION
  13. NETWORK --BOOTPROTO=DHCP --DEVICE=ENO16777728 --ONBOOT=OFF --IPV6=AUTO
  14. NETWORK --HOSTNAME=LOCALHOST.LOCALDOMAIN
  15. # ROOT PASSWORD
  16. ROOTPW --ISCRYPTED $6$PDJJF42G8C6PL069$II.PX/YFAQPO0ENW2PA7MOMKJLYOAE2ZJMZ2UZJ7BH3UO4OWTR1.WK/HXZ3XIGMZGJPCS/MGPYSSOI8HPCT8B/
  17. # SYSTEM TIMEZONE
  18. TIMEZONE AMERICA/NEW_YORK --ISUTC
  19. USER --NAME=LINUXPROBE --PASSWORD=$6$A9V3INSTNBWEIR7D$JEGFYWBCDOOOKJ9SODECCDO.ZLF4OSH2AZ2SS2R05B6LZ2A0V2K.RJWSBALL2FEKQVGF640OA/TOK6J.7GUTO/ --ISCRYPTED --GECOS="LINUXPROBE"
  20. # X WINDOW SYSTEM CONFIGURATION INFORMATION
  21. XCONFIG --STARTXONBOOT
  22. # SYSTEM BOOTLOADER CONFIGURATION
  23. BOOTLOADER --LOCATION=MBR --BOOT-DRIVE=SDA
  24. AUTOPART --TYPE=LVM
  25. # PARTITION CLEARING INFORMATION
  26. CLEARPART --NONE --INITLABEL
  27. %PACKAGES
  28. @BASE
  29. @CORE
  30. @DESKTOP-DEBUGGING
  31. @DIAL-UP
  32. @FONTS
  33. @GNOME-DESKTOP
  34. @GUEST-AGENTS
  35. @GUEST-DESKTOP-AGENTS
  36. @INPUT-METHODS
  37. @INTERNET-BROWSER
  38. @MULTIMEDIA
  39. @PRINT-CLIENT
  40. @X11
  41. %END

6. wc command

The wc command is used to count the number of lines, words, and bytes of the specified text in the format "wc (parameter) text".

Every time I talk about this order in class, there are always students who think of a kind of public facility, but there is no connection between the two. T he wc command in a Linux system is used to count the number of lines, words, bytes, and so on of text. I f in order to facilitate their own to remember the role of this command, can also be associated with the toilet is so boring, boring to count the hands of the toilet reading how many lines. The parameters of wc and the corresponding effect are shown in Table 2-10.

The parameters and effects of Table 2-10 wc

Parameter Action -l shows only the number of lines -w shows only the number of words -c shows only the number of bytes

In Linux, passwd is a file used to hold system account information, to count how many users are in the current system, you can use the following command to query, is not very magical:

[root@linuxprobe ~]# wc -l /etc/passwd 38 /etc/passwd 7. Stat command

The stat command is used to view information such as the specific storage information and time of the file in the format "stat file name".

The stat command can be used to view information such as the stored information and time of the file, and the command stat anaconda-ks.cfg shows three time states (bolded) of the file: Access, Modify, Change. The difference between the three times will be detailed in the touch command below:

[root@linuxprobe ~]# stat anaconda-ks.cfg File: ‘anaconda-ks.cfg’ Size: 1213 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 68912908 Links: 1 Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:admin_home_t:s0 Access: 2017-07-14 01:46:18.721255659 -0400 Modify: 2017-05-04 15:44:36.916027026 -0400 Change: 2017-05-04 1 5:44:36.916027026 -0400 Birth: - 8. Cut command

The cut command is used to extract text characters by "columns" in the format of "cut (parameters) text".

In Linux systems, how to accurately extract the most desired data, which is what we should focus on learning. I n general, it is relatively simple to extract data based on "rows" and only need to set the keywords to search for. B ut if you search by column, you need to use not only the -f parameter to set the number of columns you want to see, but also the -d parameter to set the interval symbol. Passwd when saving user data information, each value of the user information is spaced between the colons, and then we use the following command to try to extract the user name information in the passwd file, that is, to extract the first column of content with the colon (:) as the interval symbol:

[root@linuxprobe ~]# head -n 2 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin [root@linuxprobe ~]# cut -d: -f1 /etc/passwd root bin daemon adm lp sync shutdown halt mail operator games ftp nobody dbus polkitd unbound colord usbmuxd avahi avahi-autoipd libstoragemgmt saslauth qemu rpc rpcuser nfsnobody rtkit radvd ntp chrony abrt pulse gdm g nome-initial-setup postfix sshd tcpdump linuxprobe 9. diff command

The diff command is used to compare differences between multiple text files in the format "diff (parameter) file".

When using the diff command, you can not only use the --brief parameter to confirm that the two files are different, but also use the -c parameter to compare in detail the differences between multiple files, which is definitely a powerful tool to determine whether a file has been tampered with. For example, use the cat command to view the contents diff_A.txt the diff_B.txt the file separately, and then compare:

[root@linuxprobe ~]# cat diff_A.txt Welcome to linuxprobe.com Red Hat certified Free Linux Lessons Professional guidance Linux Course [root@linuxprobe ~]# cat diff_B.txt Welcome tooo linuxprobe.com

  1. Red Hat certified
  2. Free Linux LeSSonS
  3. ////////.....////////
  4. Professional guidance
  5. Linux Course

Next, use the diff--brief command to display the results of the comparison and determine if the files are the same:

The root@linuxprobe of the file diff_A.txt diff_B.txt Files diff_A.txt and diff_B.txt differ finally uses the diff command with the -c parameter to describe the specific differences in the contents of the file:

  1. [root@linuxprobe ~]# diff -c diff_A.txt diff_B.txt
  2. *** diff_A.txt 2017-08-30 18:07:45.230864626 +0800
  3. --- diff_B.txt 2017-08-30 18:08:52.203860389 +0800
  4. ***************
  5. *** 1,5 ****
  6. ! Welcome to linuxprobe.com
  7. Red Hat certified
  8. ! Free Linux Lessons
  9. Professional guidance
  10. Linux Course
  11. --- 1,7 ----
  12. ! Welcome tooo linuxprobe.com
  13. !
  14. Red Hat certified
  15. ! Free Linux LeSSonS
  16. ! ////////.....////////
  17. Professional guidance
  18. Linux Course