How to replace number by double of that number?
For example I have x7files and need x77files
I have sed 's/[0-9]/88/'. It replaces number by 88 so I have x88files.
I tried sed 's/[0-9]/[0-9][0-9]/' but it does not work...
try ... echo x7files | sed -e's/\([0-9]\)/\1\1/g'
Related
I have a csv file which is delimited by #~#.
there is a field which contains 0 and then n(more than 1) number of '.'(dot).
I need to remove the zero and preserve the later dots. I have to also take care that floating numbers are not affected.
So effectively replace #~#0.....#~# to #~#.....#~# (dots can be from 1 to any)
To limit the replacement with fields matching the pattern use this
$ echo "#~#0.12#~#0.....#~#0.1#~#0.#~#" | sed -r 's/#~#0(\.+)#~#/#~#\1#~#/g'
will preserve 0.12 and 0.1 but replace 0..... and 0.
#~#0.12#~#.....#~#0.1#~#.#~#
+ in regex means one or more. Anchoring with the field delimiters will make sure nothing else will be replaced.
Using sed you can do:
s='#~#0.....#~#'
sed -r 's/(^|#~#)0(\.+($|#~#))/\1\2/g' <<< "$s"
#~#.....#~#
sed -r 's/(^|#~#)0(\.+($|#~#))/\1\2/g' <<< "#~#0.00#~#"
#~#0.00#~#
]$ echo "#~#0.....#~#" | sed 's/#0/#/g'
#~#.....#~#
Escape the dots ans include all characters that should match:
echo "#~#0.1234#~#0.....#~#" | sed 's/#~#0\.\./#~#../g'
Using var's will not improve much:
delim="#~#"
echo "#~#0.1234#~#0.....#~#" | sed "s/${delim}0\.\./${delim}../g"
How could I awk, sed or tr a " " and replace it with a ",". More specifically when the number of fields for each line are different. I know how to simply sed the problem
sed 's/ /,/g'
Here's and example of a problem
Ted 36 Shaker Heights 04-25-1978
Robin 34 Vancouver 07-23-1980
Marshall 36 St. Cloud 11-28-1978
Lily 37 New York 03-22-1978
I need to sed, awk, or tr so the result becomes
Ted,36,Shaker Heights,04-25-1978
Robin,34,Vancouver,07-23-1980
Marshall,36,St. Cloud,11-28-1978
Lily,37,New York,03-22-1978
I am having trouble with the space within the city name. Any suggestions on how to fix that? The field numbers for each line is not always consist. It will either have 4 or 5 depending on the city.
If the city is always surrounded by numbers, you can just check for the transition from digits to non-digits or vice versa:
sed 's/\([0-9]\) \([^0-9]\)/\1,\2/g;s/\([^0-9]\) \([0-9]\)/\1,\2/g'
Try this:
sed -E 's/ ([0-9]+) /,\1,/;s/ ([0-9-]+)$/,\1/' file
Output:
Ted,36,Shaker Heights,04-25-1978
Robin,34,Vancouver,07-23-1980
Marshall,36,St. Cloud,11-28-1978
Lily,37,New York,03-22-1978
a dumb and basic approach that uses the greediness:
sed -r 's/^([^ ]*) ([0-9]*) (.*) /\1,\2,\3,/' file
or shorter:
sed -r 's/ ([0-9]*) (.*) /,\1,\2,/' file
how can i match the substring "2153846-11" (composed sometimes by only numbers, like "2153846", sometimes like "2153846-11" or "2153846_11", sometimes like "2153846-1" always digits and in the first group no less then 5) inside the following:
"01/16/2015","2153846-11","2015-01-16 02:50:18.0","lch_demo_hidemi-19459072-2","","01/16/2015"
and substitute the matched string with the first group (before dash/underscore) removing the second one.
The final result will be:
"01/16/2015","2153846","2015-01-16 02:50:18.0","lch_demo_hidemi-19459072-2","","01/16/2015"
The instruction will be written a unique sed line like
sed -e 's/...//g' < myfile
Thanks
You can use this sed:
sed 's/"\([0-9]*\)[_-][0-9]*"/"\1"/g' file
"01/16/2015","2153846","2015-01-16 02:50:18.0","lch_demo_hidemi-19459072-2","","01/16/2015"
You could try the below sed command.
$ echo '"01/16/2015","2153846-11","2015-01-16 02:50:18.0","lch_demo_hidemi-19459072-2","","01/16/2015"' | sed -r 's/"(2153846)([_-]11)?"/"\1"/g'
"01/16/2015","2153846","2015-01-16 02:50:18.0","lch_demo_hidemi-19459072-2","","01/16/2015"
I have string "001.036.020" and I need to convert it to "1.36.20".
Saying other words I need to remove all "0" before digit. Is it possible to do this using sed?
This sed should work:
sed 's/0*\([1-9]\)/\1/g'
EDIT: To handle more complex cases like:
0s in between digits:
handle a segment with only 0s (would be collapsed to a single zero)
On Linux:
sed -r -e 's/(^|\.)0+([1-9])/\1\2/g' -e 's/(^|\.)(0)0*(\.|$)/\1\2\3/g'
OR on Mac:
sed -E -e 's/(^|\.)0+([1-9])/\1\2/g' -e 's/(^|\.)(0)0*(\.|$)/\1\2\3/g'
echo '001.036.020' | sed 's/^0*//'
Can I increase some numbers in txt files with grep/sed?
I want to find all numbers in file and increase them for 5. Is that possible with grep and sed or I need to write app for that?
EDIT:
File has n lines which begin with number - number and than some text.
Like title for movie.
example line:
34 - 36 : Some text.
You can use perl as:
perl -i -pe 's/(\d+)/$1+5/eg' filename
See it
Probably awk. Change the record separator to whitespace (assuming this is what you want to do), then if a record matches the regex ^[0-9]*$ convert to number add 5 and print, otherwise print.
This is a pretty complete solution but "left as exercise" to code up.
I believe you should use awk Changing the Contents of a Field
>cat 1.txt
34 - 36 : Some text.
cat 1.txt | awk '{ $1=$1+5; $3=$3+5; print $0; }'
39 - 41 : Some text.
This might work for you (GNU sed & Bash):
sed 's/[0-9]\+/$((&+5))/g;s/.*/echo "&"/e' file