简体   繁体   English

如何拆分“1 $$$$”使用awk

[英]how to split “1$$$$” use awk

if I have a string like "sn":"1$$$$12056597.3,2595585.69$$", how can I use awk to split "1$$$$" 如果我有一个像“sn”这样的字符串:“1 $$$$ 12056597.3,2595585.69 $$”,我怎么能用awk来分割“1 $$$$”

I tried 我试过了

**cat $filename | awk -F "\"1\$\$\$\$" '{ print $2 }'**
**cat $filename | awk -F "\"1$$$$" '{ print $2 }'**

but all failed 但都失败了

any number of $ use 任何数量的$ use

echo '"1$$$$12056597.3,2595585.69$$"' | awk -F '"1[$]+' '{ print $2 }'

exactly 4 use 正好4次使用

echo '"1$$$$12056597.3,2595585.69$$"' | awk -F '"1[$]{4}' '{ print $2 }'

to help debug problems with escape characters in the shell you can use the built-in shell command set which will print the arguments that are being passed to awk after the shell has interpreted any escape characters and replaced shell variables 为了帮助调试shell中转义字符的问题,您可以使用内置的shell命令 ,它将在shell解释任何转义字符并替换shell变量后打印传递给awk的参数

In this case the shell first interprets \\$ as an escape for a plain $ 在这种情况下,shell首先将\\ $解释为普通$的转义

set -x
echo '"1$$$$12056597.3,2595585.69$$"'|awk -F "\"1\$\$\$\$" '{ print $2 }'
+ echo '"1$$$$12056597.3,2595585.69$$"'
+ awk -F '"1$$$$' '{ print $2 }'

You can use \\$ so the \\$ get to awk, but \\$ is interpreted in awk regular expressions as a $ anyway. 你可以使用\\ $所以\\ $得到awk,但是\\ $在awk正则表达式中解释为$ anyway。 At least awk is nice enough to warn you... 至少awk足以警告你......

echo '"1$$$$12056597.3,2595585.69$$"'|awk -F "\"1\\$\\$\\$\\$" '{ print $2 }'
+ echo '"1$$$$12056597.3,2595585.69$$"'
+ awk -F '"1\$\$\$\$' '{ print $2 }'
awk: warning: escape sequence `\$' treated as plain `$'

Turn off debugging with 关闭调试

set +x
echo '"1$$$$12056597.3,2595585.69$$"' | awk -F '"1[$]+' '{ print $2 }' |sed 's/.\{3\}$//'

Or if you want to split both float digit: 或者,如果要拆分两个浮点数:

echo '"1$$$$12056597.3,2595585.69$$"' | awk -F '"1[$]+' '{ print $2 }' |sed 's/.\{3\}$//' |awk 'BEGIN {FS=","};{print $1}'

And

echo '"1$$$$12056597.3,2595585.69$$"' | awk -F '"1[$]+' '{ print $2 }' |sed 's/.\{3\}$//' |awk 'BEGIN {FS=","};{print $2}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM