Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

search and replace with sed not working

Tags:

bash

sed

I would like to use sed for search and replace, but it doesn't seem to be working. I guess I'm making a stupid mistake I can't spot.

here is my file.txt:

PFPR03  PA1448770
PFPR03  PA1448780
PFPR03  PA1448790
PFPR03  PA1448800
PFPR03  PA1448810
PFPR03  PA1448830
PFPR03  PA1448840
PFPR03  PA1448850
PFPR03  PA1448860
PFPR03  PA1448870

I want to change PA1448770 to PA14_48770, ie. adding an underscore after the PA14.

Here are a few examples of what I've tried, none of them are affecting the file at all:

sed 's/^PA14/PA14_/g' file.txt
sed 's/^PA14([0-9]*)/PA14_\1/' file.txt 

Any help is appreciated,

like image 733
user2814482 Avatar asked Oct 21 '25 03:10

user2814482


1 Answers

You are using this token ^, which means start of string. That's either the beginning of a line, or of an entire file, but certainly not the beginning of a word. So this should work :

sed 's/PA14/PA14_/g' file.txt

edit

I highly recommend regex101 to practice your regex. You can test your expressions in live, and they have lot's of good explanations about tokens.

like image 182
John Pink Avatar answered Oct 22 '25 21:10

John Pink



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!