Click here for "Error Codes for Commercial Air Conditioners".(Japanese Version)

Extraction of strings and lines in a file

Extraction of strings and lines in a file

●Extract strings that start with a specific character

If you want to extract a specific string from a long file, use the grep command.
By specifying the string to be extracted and the file as the arguments of the grep command, and by adding "^" (carrot) as an additional condition to extract data starting from a specific string, you can extract data starting from that string.

[root@Lion ~]# grep -i "ja" country_list.txt
↑Option "-i" to extract strings containing "ja" in a case-insensitive manner
Japan
Azerbaijan
Jamaica
Libyan Arab Jamahiriya
↑Multiple string data containing "ja" will be extracted.

[root@Lion ~]# grep -i "^ja" country_list.txt
↑Use option "-i" to extract strings starting with "ja" in a case-insensitive manner
Japan
Jamaica
↑The string data starting with "ja" will be extracted.

●Extract strings ending with a specific character

In order to extract data ending with a specific string, "$" (Dollar) can be added.

[root@Lion ~]# grep -i "en" country_list.txt
↑The option "-i" extracts strings containing "en" in a case-insensitive manner
Argentina
Armenia
Benin
Central African Republic
Denmark
Grenada
Kenya
Liechtenstein
Montenegro
Saint Vincent and the Grenadines
Senegal
Slovenia
Sweden
Turkmenistan
Venezuela
Yemen
↑Multiple string data containing "en" will be extracted.

[root@Lion ~]# grep -i "en$" country_list.txt
↑Use the option "-i" to extract strings ending in "en" in a case-insensitive manner
Sweden
Yemen
↑String data ending with "en" will be extracted.

●Display a specific string including line numbers.

You can use the grep command to search for and extract a specific string in a file.
However, it does not tell you in which line of the file the string is located.
If you want to extract the search string along with the line number, run the grep command with the option "-n".

[root@Lion ~]# grep -in "en" country_list.txt ←Displays a string with line numbers.
7:Argentina
8:Armenia
19:Benin
33:Central African Republic
49:Denmark
70:Grenada
90:Kenya
100:Liechtenstein
116:Montenegro
148:Saint Vincent and the Grenadines
153:Senegal
159:Slovenia
168:Sweden
180:Turkmenistan
191:Venezuela
193:Yemen
↑A string containing "en" will be displayed with the line number.

●Extract lines that do not contain a specific string.

To exclude specific strings from the file and display them, run the grep command with the "-v" option

[root@Lion ~]# more country_list.txt
Japan
Afghanistan
Algeria
Andorra
Angola
Antigua and Barbuda
Argentina
Armenia
Australia
Austria
[root@Lion ~]# grep -iv "an" country_list.txt ←Display lines containing "an" without it.
Algeria
Argentina
Armenia
Australia
Austria
Bahamas
Bahrain
Barbados
Belarus
Belgium
Belize
Benin
↑ The results will be displayed without the line containing "an".

●Displays the number of lines that contain a specific string.

By using the "-c" option to the grep command, the number of lines containing a specific string will be displayed

[root@Lion ~]# grep -i "en" country_list.txt ←Show lines containing "en
Argentina
Armenia
Benin
Central African Republic
Denmark
Grenada
Kenya
Liechtenstein
Montenegro
Saint Vincent and the Grenadines
Senegal
Slovenia
Sweden
Turkmenistan
Venezuela
Yemen
Display the number of lines containing "en
[root@Lion ~]# grep -ic "en" country_list.txt ←Display the number of lines that contain en
16    ←There are 16 lines.
Copied title and URL