How can I search and replace a variable in Linux?

7

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.

Brad

Posted 2015-09-30T17:44:25.387

Reputation: 71

1This is a decent question. While you mention “Notepad++” in your question, the reality is this overall question holds no value to anyone looking for “Notepad++” items. – JakeGould – 2015-09-30T18:09:37.557

1Which is to say, that’s why I removed the “Notepad++” tag. – JakeGould – 2015-09-30T18:19:18.840

You say, "the VAC number will vary, so it needs to be stored as a variable".  I don't understand.  I suspect that you're overthinking this — Steven's answer should do what you want.  (And, if it does, you should *accept* it.)  But, if his answer doesn't work for you, that suggests that you haven't explained your problem completely, correctly, and clearly. – Scott – 2015-10-17T07:30:54.633

Answers

7

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####,.

Steven

Posted 2015-09-30T17:44:25.387

Reputation: 24 804