Tips for collating strings in Linux

  • 2020-12-09 01:25:08
  • OfStack

In the operation of linux, we often replace the string in the file, statistics and other operations, we will now do a collation, if there is an error, please correct.

Count the number of strings


grep -c str filename
grep -o str filename |wc -l

Substitution string

Replaces the current line matching string

:s/oldStr/newStr

Replaces all matching strings in the current file

:%s/ original string/replacement string /gg

Batch replacement string

sed-i "s/ Find field/Replace field /g" grep Find field -rl path

-ES30en represents all subdirectories


sed -i "s/new Str/old Str/g" 'grep "old Str" -rl filename'

PS: Linux commonly operates on strings

Divide the string by Spaces

awk ‘{print $1}'

Split a string with a specific character

str=${str//,/ }  ――――――�//后面是分割字符串的标志符号,最后1个/后面还有1个空格

Clipped string

 cut -b|-c|-f 3  ―――――――�b代表字节,-c代表字符,-f代表域 后面的数组是第几个字符

Remove a specific character from a string

sed ‘s/\”//g'  s代表替换,默认字符被替换为空,\后面的字符是要被替换的字符,g表示全部替换

conclusion


Related articles: