Printing files content in output
cat command
The “cat” command stands for concatenate. It is one of the most useful commands in Linux. This command displays file content in the output concatenates and creates files.
Syntax
cat [OPTION] [FILE]…………
Example
Cat without any options displays the file’s content in the output. If sending multiple files as arguments, this command will display the content of files one by one.
-n: This option displays the file content with the line number
> filename: This syntax creates a new file and waits for the user to input the file’s content. Press Ctrl+D to exit after entering the content.
-T: Separates TAB spaces in the content showing “^”.
********************-n*******************************
[root@Lpic1-CentOs9 ~]#cat -n /etc/shadow
1 root:$6$SkCMfETz6.WN32CY$iFzceHugn//b60QW14UzPV1OM9dab880DjZYV.vggD5wJZXiHbzUkygs3BiyGqKtZIstYGOG6Yu.5xuXhoYH/0:19156:0:99999:7:::
2 daemon:*:19046:0:99999:7:::
3 bin:*:19046:0:99999:7:::
4 sys:*:19046:0:99999:7:::
5 sync:*:19046:0:99999:7:::
6 games:*:19046:0:99999:7:::
7 man:*:19046:0:99999:7:::
8 lp:*:19046:0:99999:7:::
9 mail:*:19046:0:99999:7:::
10 news:*:19046:0:99999:7:::
11 uucp:*:19046:0:99999:7:::
********************>*******************************
[root@Lpic1-CentOs9 ~]# cat > readme.txt
after entering this command, it will wait for you to enter the content.
after entering the content press Ctrl+D to finish
[root@Lpic1-CentOs9 ~]# cat ./readme.txt
after entering this command, it will wait for you to enter the content.
after entering the content press Ctrl+D to finish
********************-T*******************************
[root@Lpic1-CentOs9 ~]# cat ./readme.txt
this is a TAB test
[root@Lpic1-CentOs9 ~]# cat -T ./readme.txt
this^Iis a TAB^Itest
tac Command
This command displays the file content in reverse order. It prints the content`s line from the bottom to the top.
Syntax
tac [OPTIONS]<FILE>
Example
tac without any options displays the content in reverse order.
-s or –separator STRING: it will use the string as the separator instead of the new line. It prints the content lines in reverse order until it arrives at the separator, and then it will print the content line in regular order.
[root@Lpic1-CentOs9 ~]#cat ./numbers
one
two
three
four
five
six
seven
eight
nine
ten
[root@Lpic1-CentOs9 ~]#tac ./numbers
ten
nine
eight
seven
six
five
four
three
two
one
[root@Lpic1-CentOs9 ~]# tac --separator four ./numbers
five
six
seven
eight
nine
ten
one
two
three
paste command
Paste command is a handy tool in Linux. This command is similar to the “cat” command and displays the content of files. So what is the difference between “cat” and “paste”? To display the content of several files “paste” command will merge lines of files horizontally. It uses the “tab” delimiter by default to merge files.
Syntax
paste [OPTION]… [FILES]…
Example
The “paste” command without options displays the file’s content, similar to the “cat” command. But if we give two files as arguments, it will display the content of files horizontally, merging lines using the “tab” delimiter.
-d: users can specify other characters to use as a delimiter. If users select more than one delimiter, the “paste” command will use them in circular order between lines.
-s: to merge files sequentially, use this option. It reads all the lines in a file and integrates them into a single line using the “tab” delimiter.
*************paste is similar to cat in displaying one file content************
[root@Lpic1-CentOs9 ~]#cat ./actorname
Robert Downey
Chris Hemsworth
Chris Evans
Mark Ruffalo
Tobey Maguire
[root@Lpic1-CentOs9 ~]#paste ./actorname
Robert Downey
Chris Hemsworth
Chris Evans
Mark Ruffalo
Tobey Maguire
************cat and paste differences*****************
[root@Lpic1-CentOs9 ~]#cat ./numbers ./heros ./actorname
1
2
3
4
5
Iron man
Thor
Captain America
Hulk
Spiderman
Robert Downey
Chris Hemsworth
Chris Evans
Mark Ruffalo
Tobey Maguire
[root@Lpic1-CentOs9 ~]# paste ./numbers ./heros ./actorname
1 Iron man Robert Downey
2 Thor Chris Hemsworth
3 Captain America Chris Evans
4 Hulk Mark Ruffalo
5 Spiderman Tobey Maguire
************************-d Delimiter*****************
***************one character Delimiter***************
[root@Lpic1-CentOs9 ~]# paste -d "|" ./numbers ./heros ./actorname
1|Iron man|Robert Downey
2|Thor|Chris Hemsworth
3|Captain America|Chris Evans
4|Hulk|Mark Ruffalo
5|Spiderman|Tobey Maguire
***************two characters Delimiter***************
[root@Lpic1-CentOs9 ~]#paste -d "|," ./numbers ./heros ./actorname
1|Iron man,Robert Downey
2|Thor,Chris Hemsworth
3|Captain America,Chris Evans
4|Hulk,Mark Ruffalo
5|Spiderman,Tobey Maguire
*****************PAY ATTENTION TO DelimiterS ORDER************
[root@Lpic1-CentOs9 ~]#paste -d ",|" ./numbers ./heros ./actorname
1,Iron man|Robert Downey
2,Thor|Chris Hemsworth
3,Captain America|Chris Evans
4,Hulk|Mark Ruffalo
5,Spiderman|Tobey Maguire
*****************-s Sequence**********************
[root@Lpic1-CentOs9 ~]#paste -s -d "|" ./numbers ./heros ./actorname
1|2|3|4|5
Iron man|Thor|Captain America|Hulk|Spiderman
Robert Downey |Chris Hemsworth |Chris Evans |Mark Ruffalo|Tobey Maguire