I have a huge file that has multiple columns as shown below:
J02-31 23.2 ...
J30-09 -45.4 ...
J05 30 56.1 ...
J00-20 -78.2 ...
J11-54 232.0 ...
... ... ...
I would like to replace -
with $-$
only in the first column, i.e., my output should be like this:
J02$-$31 23.2 ...
J30$-$09 -45.4 ...
J05 30 56.1 ...
J00$-$20 -78.2 ...
J11$-$54 232.0 ...
... ... ...
Is there a way to do this using vi
. I know that python/pandas
can do it, but I am interested in vi
usage.
CodePudding user response:
You could do:
:g/^\S*-/s/-/$-$/
Which performs the replacement s/-/$-$/
only on lines which match the pattern /^\S*-/
(ie, those lines which have a - in the first column).
CodePudding user response:
I'd go with
:%s/^\S*\zs-/$-$/
which means:
%s/
: apply this substitution for every line^\S*
: read as many non-whitespace characters from the start of the line as possible\zs
: actual match start (you could also capture the\S*
above instead and insert it back too)-
: match the-
(note: this will only match the last-
in the first column, your question isn't really clear if there can be multiple there)/$-$/
: replace the matching part (which is only-
thanks to the\zs
) with$-$