开发者

one line using sed and bc together?

开发者 https://www.devze.com 2023-01-01 22:16 出处:网络
I want to add one to the last value at the end of a string in sed. I\'m thinking along the lines of cat 0809_data.csv |sed -e \'s/\\([0-9]\\{6\\}\\).*\\(,[^,]*$\\)/\\1\\2/g\'| export YEA开发者_开发技

I want to add one to the last value at the end of a string in sed. I'm thinking along the lines of

cat 0809_data.csv |sed -e 's/\([0-9]\{6\}\).*\(,[^,]*$\)/\1\2/g'| export YEA开发者_开发技巧RS = $(echo `grep -o '[^,]*$' + 1`|bc)

e.g. 123456, kjhsflk, lksjgrlks, 2.8 -> 123456, 3.8

Would this be more reasonable/feasible in awk?


This should work:

years=$(awk -F, 'BEGIN{ OFS=", "} {print $1, $4+1}' 0809_data.csv)

It would be really awkward to try to use sed and do arithmetic with part of the result. You'd have to pull the string apart and do the math and put everything back together. AWK does that neatly without any fuss.

Notice that cat is not necessary (even using sed in a command similar to the one in your question) and it's probably not necessary to export the variable unless you're calling another script and need it to be able to access it as a "global" variable. Also, shells generally do integer math so you don't need to use bc unless you need floats.

0

精彩评论

暂无评论...
验证码 换一张
取 消