Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
Introduction to CAT Command in Linux
The cat command is termed “concatenate” which is one of the widely used commands in Unix/Linux & Apple Mac OS operating system. This command has different application like create single/ multiple files, view the content of the file, concatenate file and redirect their to the terminal or file. The cat command displays the file content to a screen. Cat command concatenates standard inputs to the standard . When there is no FILE or when FILE is – then it read the standard input? It performs three main roles related to manipulation of text files: creating them, displaying them & combining them.
Syntax:
cat OPTIONS FILE_NAME
Options
Examples to Implement in CAT Command in Linux
Let us discuss with the example to clarify our understanding:
Example #1
Create a New File: Using Cat command we can easily create a file. Let us create a file with name Testfile.txt and write the content “This is my First command in Linux.
Command:Cat > Testfile.txt
After adding the content press Cntrl + D to exit the shell and write the content to file.
Example #2
How to Display the Content of a File: In order to show the content of the file, we can use cat command simply and pass the name of the file/files which we want to view. The of the file will be printed on the standard and can be viewed on the terminal.
Command:cat Testfile.txt
If the contents of a file are very long, then also all the content will be written to the terminal. In such cases, it will be challenging to discover or identify all the contents. When searching for particular content grep will be a good option.
Example #3
Writing the Content of Any File to a New File: In order to write the contents of a file to a new file “shell redirection” will be used with the cat command. In the below example suppose that the file Testfile.txt has a single line with ‘This is my First Command in Linux’ and write this into File_1.txt.
Command:
cat Testfile.txt > File_1.txt
cat File_1.txt
Note: New File_1.txt is created and the content of Testfile.txt is copied to the File_1.txt.In case if the File_1.txt already exists then it will use the existing file at the path
Example #4
Append the Content of a File to Another File: If we need to append the content of any file to another file, the shell redirection will be used. For example, Consider we have two files Shop.txt and Location.txt. The file Shop.txt contains two lines.
Command:
cat Shop.txt
Retail Shop
Wine Shop
The file Location.txt file also contains two lines:
Command:
cat Location.txt
Manali,HP
Indore,MP
** Now we will use shell redirection(>>) to append the content of the two files:
Command:cat Shop.txt >> Location.txt
Command:cat Location.txt
Example #5
Merging of Multiple Files Into Unified File: In order to combine multiple files into a single file, cat command will be used with shell redirection. The below-mentioned command will combine all files with the *.txt extension inside a directory into one file in the same or different directory.
Note: * is a special character in Linux/Unix which means all.
Command:cat *.txt > merge.txt
Command:cat merge.txt
You can remember the content combined from Testfile.txt,File_1.txt,Shop.txt and Location.txt as all have the .txt extension.
Example #6
Adding Line Number to the Cat : To add line number to the of cat command we will use the -n option. It will prepend the numerical value to the start of each line.
Command:cat -n merge.txt
You can see the numbers are given to each line in ascending order.
Example #7
Showing the End of the Line: If we want to know the end of lines in a file then we will use cat command with the -E option. It will append a “$” character to the end of the lines. This option is useful to check when there is trailing space in the file. For example, let us use the same merge.txt.
Command:cat -E merge.txt
Now let us add some lines with space then we will see a tab space between the end line and “$”
Example #8
Squeezing or Compressing of Blank Lines: In order to squeeze/compress blank lines in a file, we can use a cat with the -s option. It will squeeze the blank line then suppress repeating empty lines. Suppose we have file line.txt, which contains the below data.
Command:cat -s line.txt
We can see that duplicate records have been removed.
Example #9
Showing Tabs in the File: To show tab we can use cat with -T option. It will show tabs as ^I.
File tabs.txt contains:
Command:cat -T tabs.txt
Example #10
Version
Command:cat –version
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.