Linux command # 4

linux command # 4.

grep command

Search for characters gerep [option] Extract lines that match from .

[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

sed command

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

awk command

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

Linux command # 4
Linux command # 3
Linux command # 5
Linux command list
linux at command
[Linux] Search command
Linux command <Basic 2>
Linux [directory command]
Linux server command
Linux # Command Memo 1
Linux command [read]
Linux Command Summary
[Basic] linux command
Linux [shell command]
[Linux] Command / Knowledge
My linux command
Linux command <Basic 1>
Linux command collection
Linux mkdir command
Linux command basics
[Linux] Git command
Linux (command memory)
[Linux] Volume configuration command
Linux command cheat sheet
Linux command (sequential update)
Linux basic command memorandum
Linux command [File operation]
[Linux] Basic command summary
Linux
Linux command for self-collection
linux command error collection 1
Linux command line shortcut
linux sar command CPU usage
[Linux] tar.gz compression / decompression command
What is Linux? [Command list]
Easy df command on Linux
Linux tar xz command memo
Linux Command Dictionary (for myself)
linux: create original Terminal command
[Note] Useful linux command collection
Linux command memorandum [for beginners]
Linux PC spec check command
[Linux] User / group command summary
[Infrastructure] Linux command, shell script collection
linux memorandum
Linux commands
[Linux convenient command] Try inserting exa
Linux commands
Linux overview
Linux basics
Command to create Linux Live USB
Command memorandum
direnv (linux)
nkf command
Organize Linux
command prompt
[Linux] OS recovery with restore command
linux commands
Linux practice
Ubuntu Linux 20.04
Completion of docker command on Linux