Using sed to modify a header - sed

Supposed I had a file that has the header:
exm2240_T exm4561_G exm1916_0 exm490_1 rs67856512_A
I was wondering how to get the header to appear like this using sed:
exm2240 exm4561 exm1916 exm490 rs67856512

Just remove the underscore and the character that follows it:
sed 's/_.//g'

Related

How to replace strings in all files using sed?

I want to replace below line with next line in all the files. So what sed pattern is used for this. I have tried lot but not figured that out..
checkToken($token['token'])
checkToken($token)
This is what I have tried
sed -i -- 's/checkToken\(\$token\['token'\]\)/checkToken\(\$token\)/g' get_officers_v2.php
You just need to get your escape-characters (\) on the right place like:
sed -ie "s/\(checkToken(\$token\)\['token'\])/\1)/" get_officers_v2.php

sed to replace a string which consist of a forwardslash

I'm trying to replace below specific lines in a file
/ACCOUNT/passwd=
/BMC/CONFIRMATION/PASSWORD=
I need help in preparing the sed command
The required output would look something like this
/ACCOUNT/passwd=-2$-$A88CA7BD3DADDDFFC
/TMC/CONFIRMATION/PASSWORD=-2$-$A88CA7BD3DADDDFFC
Any help is appreciated.
There is nothing special about the forward slash, except if you choose to use it as the delimiter in your sed command, so don’t:
sed 's,ACCOUNT/passwd=,ACCOUNT/passwd=-2$-$A88CA7BD3DADDDFFC,g'
And similar for other target strings.
Here I’ve used a comma as the delimiter. You can choose another character as you prefer.

Sed replace specific substring

I have a file that's generated as an output to an SQL query. I need to replace the nulls in the file with blanks, so something like
sed -e"s/null//g" would work.
However there's a valid string of the form 'null/' (with a trailing forward slash) and that should not be replaced. Is there a way to replace only 'null' values while leaving 'null/' intact?
The sed one-liner:
sed 's#null\([^/]\|$\)#\1#g' file
should work for your requirement.
It searches pattern: null and followed by a non-slash char (or EOL),
replace with the followed non-slash char.
Thus, null/ won't be touched.
I think this command should be enough:
sed -e "s/null[^/]//g"

How to cut prefix of string with 'sed'?

Consider the following lines:
prefix1.value[TAB]someString
prefix2.anotherVal[TAB]anotherString
val[TAB]String
pref.stuff[TAB]stuff
dontTouch[TAB]stuff
I would like to have the result
value[TAB]someString
anotherVal[TAB]anotherString
val[TAB]String
stuff[TAB]stuff
dontTouch[TAB]stuff
So I want to cut the prefix. if there is one. Regular expressions work in the way that the first match is the longest so I was not able to create a working program. Is it possible to do this task with a single sed program?
My solution that is not working as it should:
sed 's/^[^\t\.]*\.\?\([^\t\.]\+\)\t\(.*\)/\1\t\2/'
This matches the prefix alone, and replaces it by an empty string.
sed 's/^[^\t\.]*\.//'
Try this if there is only one dot possibe:
sed -e 's/^.*\.//' file
if until first dot
sed 's/^[^.]*\.//' YourFile
if until last dot
se 's/.*\.//' YourFile
up to you to define your prefixe type

Sed replace error

I have a pattern I am trying to match:
<x>anything</x>
I am trying to replace 'anything' (which can be any text, not the text anything - (.*)) with 'something' so any occurrences would become:
<x>something</x>
I am trying to use the following sed command:
sed "s/<x>.*</x>/<x>something</x>/g" file
I am getting the following error:
sed: -e expression #1, char 19: unknown option to `s'
Can someone point me in the right direction?
This might work for you (GNU sed):
sed -r 's/(<x>)[^<]*/\1something/g' file
This looks to replace <x> and something which is not a < by <x>something repeatedly on the same line.
N.B. .* is greedy and may well swallow up further tags on the same line.
The slashes in the closing XML tags are confusing it. Try escaping them like this:
sed "s/<x>.*<\/x>/<x>something<\/x>/g" file
You can apparently also use an equals sign which I'd never seen before. I'll be changing a bunch of scripts when I get to work!