Replace all entries in a row
-
Hi everyone, I have a
.txtfile containing multiple rows and columns. For a particular row, I want to keep the first value unchanged and replace every other value in that row with a fixed number.Example:
Original row:
Ns 0 1 2 3 4Desired row:
Ns 1 1 1 1 1Any suggestions would be appreciated—thanks!
-
Unfortunately, your example data (both before and after) wasn’t good enough to clarify what you wanted.
There are lots of regex that will do what you want on that specific piece of data. But until you define what you actually want under multiple conditions, it will be impossible to make you happy.
For example,
- Is
Nswhat causes it to be “a particular row” - Is it possible for there to be “a particular row” that has something other than five numbers
- Are all your numbers single digits? Or can some of them be multiple digits (like
Ns 0 11 2 33 4444)? - Are there any spaces before the
Ns? - Are those spaces or tabs between columns?
The best advice for asking for search/replace help is to give a block of data, showing both things that change, and things that should stay the same.
For example,
Ms 0 1 2 3 4 Ns 0 1 2 3 4 Ps 0 1 2 3 4would work (by my definition, based on my interpretation of your incomplete spec) with
- FIND =
^(Ns) \d \d \d \d \d - REPLACE =
$1 1 1 1 1 1 - SEARCH MODE =
Regular Expression
ending up with
Ms 0 1 2 3 4 Ns 1 1 1 1 1 Ps 0 1 2 3 4… but it would do nothing to the text
Ms 0 1 2 3 4 Ns 0 11 2 33 4444 Ps 0 1 2 3 4Assuming the rule is “match a line starting with Ns followed by 5 integers of 1 or more digit each”, the FIND would be
^(Ns) \d+ \d+ \d+ \d+ \d+and the REPLACE would be as I described above. That updated FIND would then turn the “do nothing” text the same way my original did.But it all depends on what your real data looks like.
----
Useful References
- Is