fzd
May 16, 2012, 10:44am
1
Hello,
Here's a file of mine:
key1:431
key2:159
key3:998
I need to change these keys to something bigger - and I actually need to shift them all by a range of 3.
The output would be:
key1:434
key2:162
key3:1001
I can't find the propper sed/awk line that would alter all my lines.
The plan-B would be to parse each line, get the value, increment it, and replace it. But I'm sure there's something smarter. .
Thanks
fzd:
The plan-B would be to parse each line, get the value, increment it, and replace it. But I'm sure there's something smarter. .
Actually there isn't and its almost a one-liner (at least for a file as simple as you have presented it).
I hate to admit it but arithmetics in sed is beyond its capabilities. I already tried that for fun but gave up.
bakunin
if all you have is key:val then awk is good at math:
$ printf '%s\n' key1:431 key2:159 key3:998 | awk -F: '{print $1 FS 3+$2}'
key1:434
key2:162
key3:1001
fzd
May 16, 2012, 11:19am
4
My file is a bit tricker than that (most of the lines don't have the key:value thing, and the "key:value" thing is more like "id1-id2-key_value_comment-id3_id3" (- and _ are "separators", but I suppose -F with awk will do what I want)
Example of the file:
name-firstname
number-street-zip_city
phone_email
eggs_value1_discounts-Y/N
tomatoes_value2_discounts-Y/N
bread_value3_discounts-Y/N
wholebread_value4_discounts-Y/N
etc.
We need to increase all the bread things when someone buys some bread.
You can use [-_]
as field sep. And possibly filter those lines with NF==7
. Let us know if you need more help.