Linux – How to search and replace a variable in Linux

bashlinuxregex

I have a bunch of files with lots of rows configured like this:

what,r-crq,What,What,VAC5700-035080,What
i,pns11,I,I,VAC5700-035090,I
do,vdd,did,did,VAC5700-035100,did

I want to do a search and replace to end up with this, storing and reinserting the 4 numbers after VAC and removing the trailing portion of the VAC number:

what,r-crq,What,What,VAC5700,What
i,pns11,I,I,VAC5700,I
do,vdd,did,did,VAC5700,did

Is there a way to do this for all the files at once (the VAC number will vary, so it needs to be stored as a variable), preferably in the Bash shell?

I assume I could also do this in “Notepad++” using regex, but I think the shell script would be preferable as I could do all the files as a batch.

Best Answer

Use the sed command:

sed -e 's|,VAC\([0-9][0-9][0-9][0-9]\)-[0-9]*,|,VAC\1,|' inFile > outFile

It will substitute ,VAC####-#...#, with ,VAC####,.

Related Question