Using awk
Assuming that your input file is tab-separated:
$ awk -v OFS="\t" '$1=="rs4648843"{$4="ADS"} 1' file
rs4648841 chr1 2365885 -- A T 0.40095 0.228978043022122 chr1:2523811
rs4648843 chr1 2366316 ADS T C 0.15694 0.5736208829426915 chr1:2523811
rs61763906 chr1 2366517 -- A G 0.07726 0.5566728930776897 chr1:2523811
To change the existing file:
awk -v OFS="\t" '$1=="rs4648843"{$4="ADS"} 1' file >file.tmp && mv file.tmp file
Using sed
Again, assuming tab-separated input, to change the file in-place:
sed -i -r '/^rs4648843/ {s/(([^\t]*\t){3})[^\t]+/\1ADS/}' file
The above was test on GNU sed. For OSX (BSD) sed, try:
sed -i .bak -E '/^rs4648843/ {s/(([^\t]*\t){3})[^\t]+/\1ADS/;}' file
Using awk but passing in the rs...
value as a variable
awk -v rs="rs4648843" -v OFS="\t" '$1==rs{$4="ADS"} 1' file
Using sed With a String With a Slash
As per the comments, suppose that, instead of ADS
, we want to substitute in TRAF6-RAG1/2
. Since this contains a /
character, it will confuse the sed
command given above. There are two possible solutions: one is to escape the /
with a backslash. This works as follows:
sed -r '/^rs4648843/ {s/(([^\t]*\t){3})[^\t]+/\1TRAF6-RAG1\/2/}' file
The other solution is to use a different marker for the substitution command. sed's substitution commands are often written in the form s/old/new/
but other markers besides /
are possible. As an example, the following use a vertical bar, |
, as the marker instead of /
, and thus accommodates the new string:
sed -r '/^rs4648843/ {s|(([^\t]*\t){3})[^\t]+|\1TRAF6-RAG1/2|}' file