I have data that looks like this, where the negative sign is a suffix at the end of the numeric.
"general_amount"
"0000000441244"
"0000000127769-"
"0000000043819"
"0000000522600-"
Can someone help me find a regular expression to produce the desired output below.
"general_amount"
441244
-127769
43819
-522600
sub('^0*([^-]*)(-?)$', '\\2\\1', x)
## [1] "general_amount" "441244" "-127769" "43819" "-522600"
^0*
matches all leading 0
characters.
[^-]*
matches all non- -
characters.
-?
matches zero or one -
character.
Finally, the $
matches the end of the string.
The middle two pieces are captured with ()
, as \\\\1
and \\\\2
, and printed in reverse order.
Using gsub , with another idea.
The idea is to divide the input into 3 elements
find the '-' 1 or zero times : (-?)"
as.numeric(gsub("(^0+)([0-9]+)(-?)","\\\\3\\\\2",tt)) [1] 441244 -127769 43819 -522600
Dude it took me 3 hours to find answer to your question
sed -re 's/[^a-zA-Z0-9]0+([0-9]+)(-?)/\2\1/g' anyfile.txt
But in the end i did it. May have some short coming but i got it nearly
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.