linux command # 4.
Search for characters
gerep [option]
[wataru@localhost work]$ cat work.02.txt
2020/07/17
test?cat
123445
[wataru@localhost work]$
[wataru@localhost work]$ grep 17 work.02.txt
2020/07/17
[wataru@localhost work]$ grep -n test work.02.txt
#-You can output the number of lines by specifying n
2:test?cat
[wataru@localhost work]$ grep -i test work.02.txt
#-If i is specified, it is not case sensitive
test?cat
TEST?CAT
test
testcat
[wataru@localhost work]$ grep -v test work.02.txt
#-Display unmatched lines when v is specified
2020/07/17
123445
TEST?CAT
[wataru@localhost work]$ grep 't[ef]*' work.02.txt
#If you want to specify a specific string,[]Use the metacharacter
test?cat
test
testcat
tfst
[wataru@locsalhost work]$ grep 'test[01-10]' work.02.txt
#test01~Search up to 10 is specified
test01
test02
test03
test04
Stream editor (non-interactive editor)
[wataru@localhost work]$ cat work.02.txt
#work.02.Total amount of txt
2020/07/17
test?cat
123445
TEST?CAT
test
testcat
tfst
test01
test02
test03
test04
[wataru@localhost work]$ sed 1d work.02.txt
#Meaning to delete the first line with 1d
test?cat
123445
TEST?CAT
test
testcat
tfst
test01
test02
test03
test04
[wataru@localhost work]$ sed 1,5d work.02.txt
#,[comma]Can be specified as "from the nth line to the mth line"
testcat
tfst
test01
test02
test03
test04
[wataru@localhost work]$ sed '4,$d' work.02.txt
#$It is possible to delete up to the last line by specifying
#4th to last line
2020/07/17
test?cat
123445
[wataru@localhost work]$ sed -n 2p work.02.txt
#To p to display the line-Add the n option to print a specific line
test?cat
Pattern search Edit text search, extraction, processing, etc.
[wataru@localhost work]$ ls -l
total 8
drwxrwxr-x. 2 wataru wataru 6 Jul 5 04:38 gogodur
drwxrwxr-x. 3 wataru wataru 156 Jul 17 04:25 testgo
-rw-rw-r--. 1 wataru wataru 83 Jul 29 05:56 work.02.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.03.txt
-rw-rw-r--. 1 wataru wataru 25 Jul 21 04:32 work.04.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.05.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.06.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.07.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.08.txt
-rw-rw-r--. 1 wataru wataru 0 Jul 5 04:09 work.09.txt
[wataru@localhost work]$ ls -l | awk '{print $1}'
#A commonly used process in awk is column selection, which extracts and displays specific fields.
#Extracting field 1
total
drwxrwxr-x.
drwxrwxr-x.
-rw-rw-r--.
-rw-rw-r--.
-rw-rw-r--.
-rw-rw-r--.
-rw-rw-r--.
-rw-rw-r--.
-rw-rw-r--.
-rw-rw-r--.
[wataru@localhost work]$ ls -l | awk '{print $1,$9}'
#It is also possible to select multiple fields
total
drwxrwxr-x. gogodur
drwxrwxr-x. testgo
-rw-rw-r--. work.02.txt
-rw-rw-r--. work.03.txt
-rw-rw-r--. work.04.txt
-rw-rw-r--. work.05.txt
-rw-rw-r--. work.06.txt
-rw-rw-r--. work.07.txt
-rw-rw-r--. work.08.txt
[wataru@localhost work]$ echo 100 300 | awk '{print $1}'
100
[wataru@localhost work]$ echo 100 300 | awk '{x=$1*$2; print x}'
#awk can also be calculated
#It is also possible to store the calculation result in the x variable and output the result.
30000
[wataru@localhost work]$ seq 1 10 | awk '{x=x+$1; print x, $1}'
#Output numbers from 1 to 10 with the seq command
#Add the numbers from 1 to 10 of the input with the awk command and output the result
1 1
3 2
6 3
10 4
15 5
21 6
28 7
36 8
45 9
55 10
Recommended Posts