Skip to content
Advertisement

How to remove commas in the middle of a specific line in a file in linux/unix

Someone was trying to be helpful in their test description. However, they added commas to the description so that when the test description is outputted to the log file, the results have extra commas. This makes it difficult to parse the results since the number of commas vary in the results file.

I want to use sed and go into the test files to remove the commas from the description so we don’t get bitten in the butt anymore, but I’m not sure what the regex should look like since I need to preserve everything else and remove just the commas. The line is from a jmeter jmx file.

Here are a few sample lines:

1 comma

HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="avgRespTime inst = green, 12 hr" enabled="true">

2 commas

HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="avgRespTime, inst = network, 2 days" enabled="true">

Can someone give me a hint on how to search for this line and remove only commas while keeping everything else intact? Thanks in advance for any help you can give me.

EDIT: There might be other lines in the jmx file that contain a comma too so I can’t blindly say something like:

sed -i 's/,//g' file.jmx

Advertisement

Answer

You can use tr to remove all commas from a given string:

s='HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="avgRespTime, inst = network, 2 days" enabled="true">'
tr -d <<< "$s"

Or to change it inline using sed in all the lines that have HTTPSamplerProxy text :

sed -i.bak '/HTTPSamplerProxy/s/,//g' file
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement