一行同时使用 sed 和 bc ?
我想在 sed 中字符串末尾的最后一个值上加一。 例如
cat 0809_data.csv |sed -e 's/\([0-9]\{6\}\).*\(,[^,]*$\)/\1\2/g'| export YEARS = $(echo `grep -o '[^,]*$' + 1`|bc)
我正在思考 123456、kjhsflk、lksjgrlks、2.8 -> 123456, 3.8
这在 awk 中会更合理/可行吗?
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 YEARS = $(echo `grep -o '[^,]*
e.g. 123456, kjhsflk, lksjgrlks, 2.8 -> 123456, 3.8
Would this be more reasonable/feasible in awk?
+ 1`|bc)
e.g. 123456, kjhsflk, lksjgrlks, 2.8 -> 123456, 3.8
Would this be more reasonable/feasible in awk?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可行:
尝试使用 sed 并对部分结果进行算术运算确实很尴尬。你必须把绳子拉开并进行数学计算,然后将所有东西重新组合在一起。 AWK 干脆利落地做到了这一点,没有任何大惊小怪。
请注意,
cat
不是必需的(即使在与您问题中的命令类似的命令中使用sed
),并且可能不需要导出变量,除非您调用另一个脚本并需要它能够将其作为“全局”变量进行访问。另外,shell 通常执行整数数学运算,因此除非需要浮点数,否则不需要使用 bc 。This should work:
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 usingsed
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 usebc
unless you need floats.