Replace string in multiples files in a directory

Hello,
I need to find and replace a single string lin_np with lin_p in the content of about 700+ .cfg files in a directory. Could someone please help me with this? Thanks in advance and regards!

@Gedimin , what have you tried , please share your attempts first, then we can look to assist.

thks

@Gedimin
... also search for similar threads/topics in this forum - there was a similar thread very recently...

1 Like

Here is what I have in mind. Maybe you guys can correct me if I am wrong. I need to replace lin_np with lin_p

cd /path/to/folder/with .cfg files/
sed -i '.cfg' 's/lin_np/lin_p/g' *

@Gedimin,
here's a snippet from GNU man sed :

       -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if SUFFIX supplied)

I think you're misinterpreting the spec of the -i and it's meaning - see if the above man helps.
Also it might help if you provided a representative sample of one of your cfg files.

Please you markdown code tags when posting code/data samples - markdown specs are described here - I've edited your post for now, but do properly format your posts going forward.

If the directory name has really got spaces then put it in quotes.

cd "/path/to/folder/with .cfg files/"

The argument after -i is a "backup" like .bak or .orig
And there must not be a space, because the argument is optional.
Without a backup:

sed -i 's/lin_np/lin_p/g' *.cfg

The lin_np can be a part of a word. Often you want it to be a whole word, and you can put "word boundary markers":

sed -i 's/\<lin_np\>/lin_p/g' *.cfg

Linux might also take \b as a boundary marker.

Thank you. The command worked perfectly

sed -i 's/lin_np/lin_p/g' *.cfg

This topic was automatically closed 300 days after the last reply. New replies are no longer allowed.