京橋のバイオインフォマティシャンの日常

南国のビーチパラソルの下で、Rプログラムを打ってる日常を求めて、、Daily Life of Bioinformatician in Kyobashi of Osaka

Grep command for string processing (1) Processing of text in a file and other

About grep command

grep command is used in order to retrieve or search for a certain string.

Broadly speaking, the way you do the grep command depends on whether you grep the text in the file or the file name.

It's simple, but it can be applied to many things.

By the following command, you can download the files/folders for practice *1.

#Download
svn export  https://github.com/kumeS/Blog/trunk/grep_practice

#Move to the working folder
cd ./grep_practice

Processing of text in files

Show lines with a certain string

#Display the file
cat test_01.txt

#Output the lines containing 'abc'
grep 'abc' test_01.txt
#OR
grep "abc" test_01.txt
#OR
cat test_01.txt | grep 'abc'

Show lines without the certain string

#Output the lines without 'abc
grep -v 'abc' test_01.txt 
#OR
cat test_01.txt | grep -v "abc"

Output lines without a certain string as an alias file

#Save the lines without 'abc' as 'test_01_v.txt'.
grep -v 'abc' test_01.txt > test_01_v.txt

#Display of test_01_v.txt
cat test_01_v.txt

Option

-v: search for a string that does not contain or does not match

Delete blank lines

#Show the lines without blank lines
cat test_02.txt | grep -v '^\s*$'

#To save as an alias file
cat test_02.txt | grep -v '^\s*$' > test_02_r.txt

Option

^: beginning of line

\s: empty word

^\s*: Zero or more repeated spaces from the beginning of the line.

Count the number of lines in a file

#Counting the total number of lines
cat test_01.txt | grep '' -c

#How not to use grep
cat test_01.txt | wc -l

#Counting only the number of lines containing 'abc'
cat test_01.txt | grep 'abc' -c

Option

-c: Counting lines or stuffs.

Searching for strings with multiple criteria

cat test_02.txt

#AND search
#Showing lines that contain 'AAA' and 'ABB' in the companion
grep 'AAA' test_02.txt | grep 'ABB'
cat test_02.txt

#OR search (-e notation)
#Display lines containing 'AAA' or 'BBB'
grep -e 'AAA' -e 'BBB' test_02.txt

#OR search (regular expression)
#Display lines containing 'AAA' or 'BBB'
grep 'AAA\|BBB' test_02.txt

Option

-e: Specify a search pattern

-w: Search for a pattern match across words

-x: Find a whole line that matches the pattern

-i: It's not case sensitive.

-n: Show line numbers in the search results

Processing of file names

Search for file names with a certain string

#Display of files in the directory
ls

#Display the .txt file only
ls | grep '.txt'

#counting .txt file
ls | grep '.txt' | wc -l

#Display the line, word and byte counts in the file
ls | grep 'test_01.txt' | wc
#OR
wc test_01.txt

Option

-l: Targeting file names

Search including subdirectories

#Counting the number of files containing '.pdf'
ls | grep '.pdf' | wc -l

#Show files including subdirectories
du -a

#Display of files containing '.pdf', including subdirectories
du -a | grep '.pdf'

#Count the number of files, including subdirectories
du -a | grep '.pdf' | wc -l

Search and delete certain files

#Delete files that contain '.pdf' in the current directory
ls | grep '.pdf' | xargs rm -rf

#Delete files containing '.pdf' in the subdirectories
du -a | grep '.pdf' | xargs rm -rf

Here, xargs means the result of the previous command is passed to the next command rm as an argument.

Supplement info

Terminal Shortcuts

Move the cursor
Ctrl + b backward / backward a word
Ctrl + f move forward and one word forward
Ctrl + a move to the beginning of the line
Ctrl + e move to end of line
Delete
Ctrl + w delete a single word or phrase
Ctrl + k delete to end of line
Ctrl + u delete to the beginning of the line
Ctrl + d delete a character in the cursor
Ctrl + h remove one character after the cursor
History
previous command history
next command history
History History
Other
Ctrl + c kill the current command
Ctrl + z pause the running command
Ctrl + d Exit, Logout
Ctrl + l Clear the screen
Ctrl + t change the cursor's character with the previous one
Ctrl + m, Ctrl + j, Ctrl + o Enter

cat : view, merge, and create for file

#Display in the text file (test.txt)
cat test.txt

#Add line numbers and display the file
cat -n test.txt

#Create a combined file from multiple files
cat test1.txt test2.txt > test3.txt

#Create an empty file (press Ctrl + d to exit)
cat > test4.txt

#All history displays, "~ (tilde)" means the home directory
cat ~/.bash_history
#OR
history

head / tail : show the beginning and end of the file

#Display 10 lines from the top of the file.
head -n 10 test.txt

#Display 10 lines from the end of the file
tail -n 10 test.txt

Terminal Commands You Should Remember

#Terminate the system
shutdown -f now
              
#Reboot
reboot

#Give all users execute privileges
chmod a+x test.command

#Executing commands as the Root user
sudo ...

Other Terminal Commands

#Display the date and time
date

#View 2012 Calendar
cal 2020

#Show Active Jobs
jobs

*1:To run the command, delete the "$".