Some usage skills of sed (pit encountered)

The sed command needs to use single quotes

The command of sed must use single quotes, not double quotes, otherwise various errors will occur

print some lines specified

sed -n ‘2,5p; 8p; 12,15p; 17p; 19p; 21,27p’ infile

  • -nPrevent sed from printing all lines
  • 2,5 is 2~5 rows

Use regular expressions instead, use group, and note that \d cannot be used to represent numbers

-eSometimes many characters need to be escaped, for example (, +, ?, ,
but -Esometimes characters need not be escaped, which is very important. Because -e and -E want to achieve the same function, the commands are different, and it will be troublesome if they are confused.

Regular description:

  • *means match 0 or more (in -eyes \*)
  • +means match 1 or more (in -eyes \+)
  • ()Inside is the matching group (in -eis \(, \))
  • &and \0both means the entire match
  • \1, \2indicating the first and second matching groups
  • Note (there are pitfalls)\d , there are other uses in sed , and it cannot be used to represent numbers. See the reason https://www.gnu.org/software/sed/manual/sed.html. To represent numbers, you need to use \digit(seemingly?)

Example
I want to change l1_tlb0,, l1_tlb1etc. to 0,, 1,
use-E

echo 'index,l1_tlb0,l1_tlb1' | sed -E 's/(l1_tlb)([0-9]+)/\2/g'
index,0,1

use-e

$ echo 'index,l1_tlb0,l1_tlb1' | sed -e 's/\(l1_tlb\)\([0-9]\+\)/\2/g'
index,0,1

Note that the above is [0-9] to represent numbers, remember that \dit cannot be used to represent numbers, otherwise it will not change after running like below

$echo 'index,l1_tlb0,l1_tlb1' | sed -E 's/(l1_tlb)(\d+)/\2/g'
index,l1_tlb0,l1_tlb1

Guess you like

Origin blog.csdn.net/qq_29809823/article/details/129388654