mfk534
mfk534

Reputation: 719

Change character in only one column in a file

I have a file that looks like this

0.9216  .   0
0.6774  .   1
0.7954  .   3
0.6375  .   1
0.6262  .   1

And I would like to replace the second column "." with a different character, a "+".

I can do this using sed with:

sed 's/[.]/+/g' 

but then the "." in my first column also change to plus signs. I've been thinking that using awk might allow me to apply this syntax to only one column, but I haven't been able to get the syntax right. Any suggestions? Thank you!

Edited to add additional data: Both of the below solutions work with the abbreviated data above, but neither works with expanded data, which looks something like this:

dm  G   0.874   1   358 440 12  126890980   .   0
dm  G   0.8253  0.9582  358 440 4   57561647    .   1
dm  A   0.5438  0.9531  358 440 4   85161551    NA  1
dm  T   0.4991  0.8726  358 440 4   108826383   .   1
dm  A   0.7246  0.9817  358 440 4   114553253   .   1
dm  C   0.7691  0.9125  358 440 4   172776204   .   1

Why would that be? I've tried specifying spaces to tabs, but they're both tab delimited.

Upvotes: 0

Views: 339

Answers (4)

BMW
BMW

Reputation: 45293

Using awk

awk '{$(NF-1)=$(NF-1)=="\."?"+":$(NF-1)}1' file

Upvotes: 0

Jotne
Jotne

Reputation: 41460

You can use awk and specify what column you like to change like this:

awk '{sub(/\./,"+",$9)}1' file
dm G 0.874 1 358 440 12 126890980 + 0
dm G 0.8253 0.9582 358 440 4 57561647 + 1
dm  A   0.5438  0.9531  358 440 4   85161551    NA  1
dm T 0.4991 0.8726 358 440 4 108826383 + 1
dm A 0.7246 0.9817 358 440 4 114553253 + 1
dm C 0.7691 0.9125 358 440 4 172776204 + 1

Upvotes: 1

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

Simply add spaces to distinct.

> cat "0.9216  .   0" | sed "s# \. # \+ #"
"0.9216  +   0"

For expanded data you could use:

> cat "dm  T   0.4991  0.8726  358 440 4   108826383   .   1" | sed -r "s#(\s+)\.(\s+)#\1\+\2#"
"dm  T   0.4991  0.8726  358 440 4   108826383   +   1"

\s+ match any white space character [\r\n\t\f ] Quantifier +: Between one and unlimited times, as many times as possible, giving back as needed. \. matches the character, literally. All matched whitespace is preserved, only the dot is replaced with +.

Upvotes: 1

jaypal singh
jaypal singh

Reputation: 77145

Using sed: Put spaces around it to segregate it from your decimal points.

$ sed 's/ [.] / + /' file
0.9216  +   0
0.6774  +   1
0.7954  +   3
0.6375  +   1
0.6262  +   1

Using awk: Though you will loose additional spaces as when you modify a column, awk rebuilds the line with default OFS of space.

$ awk 'sub(/[.]/," + ",$2)' file
0.9216  +  0
0.6774  +  1
0.7954  +  3
0.6375  +  1
0.6262  +  1

Upvotes: 1

Related Questions