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
-n
Prevent 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
-e
Sometimes many characters need to be escaped, for example (
, +
, ?
, ,
but -E
sometimes 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-e
yes\*
)+
means match 1 or more (in-e
yes\+
)()
Inside is the matching group (in-e
is\(
,\)
)&
and\0
both means the entire match\1
,\2
indicating 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 reasonhttps://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_tlb1
etc. 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 \d
it 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