I have a string with comma separated values, like:
As you can see, the 3rd comma separated value has sometimes special character, like the dash (-), in the end. I want to used sed, or preferably perl command to replace this string (with the -i option, so as to replace at existing file), with same string at the same place (i.e. 3rd comma separated value) but without the special character (like the dash (-)) at the end of the string. So, result at above example string should be:
Since such multiple lines like the above are inside a file, I am using while loop at shell/bash script to loop and manipulate all lines of the file. And I have assigned the above string values to variables, so as to replace them using perl. So, my while loop is:
while read mystr do myNEWstr=$(echo $mystr | sed s/[_.-]$// | sed s/[__]$// | sed s/[_.-]$//) perl -pi -e "s/\b$mystr\b/$myNEWstr/g" myFinalFile.txt done < myInputFile.txt
$mystr is the "SOME-STRING_A_-BLAHBLAH_1-4MP0-" $myNEWstr result is the "SOME-STRING_A_-BLAHBLAH_1-4MP0"
Note that the myInputFile.txt is a file that contains the 3rd comma separated values of the myFinalFile.txt, so that those EXACT string values ($mystr) will be checked for special characters in the end, like underscore, dash, dot, double-underscore, and if they exist to be removed and form the new string ($myNEWstr), then finally that new string ($myNEWstr) to be replaced at the myFinalFile.txt, so as to have the resulting strings like the example final string shown above, i.e. with the 3rd comma separated sub-string value WITHOUT the special character in the end (which is dash (-) at above example).
After investigation and much testing, I have concluded that problem is that at such cases for some reason the $mystr cannot be parsed to be replaced, because, for example, it has the special character at the end of the existing string in file (dash (-) at our above example)!!
So, I am desperately looking for a way to replace such string with special character in the end, with the same string WITHOUT the special character in the end at the file and at the string line in question, at exactly the same place, i.e. at the 3rd comma separated value of the string line.