简体   繁体   中英

Replace a line with a number if part of it is matches using sed

I know this is a very simple question and been discussed many times, but I can't understand where I am doing wrong in my command.

I would like to replace the lines which starts with "It" as 99999. Each row starts with several blank spaces.

infile.txt
     3
     2
     3
     4
  It is not a number = /home/kayan/data
     3
     5
  It is not a number = /home/kayan/data
     4
     5

I used

sed -i 's/^I/99999/g' infile.txt

But it is not working.

Due to starting space, add it to pattern search

sed -i 's/^[[:blank:]]*I.*/99999/' infile.txt

using the change function

sed -i '/^[[:blank:]]*I/ c\
9999' infile.txt

keeping starting space

sed -i 's/^\([[:blank:]]*\)I.*/\199999/' infile.txt

No need of the g , there is only 1 change per line possible

What you are replacing there is just the ^I part, ie the first letter. Use ^I.* instead to match the whole remaining line and it also gets replaced.

试试看:

sed -i 's/^\s*It.*/9999/'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM