我需要将下面的awk命令转换成sed,
awk -F ',' '$2 ~ /^ *[0-9]*(\.[0-9]+)?" *$/{sub(/"/, "", $2); print $2}'
以下是我的input文件:
sample.txt的:
3",3"
6-position,6-position
7' 4" to 10' 3-1/2",7' 4" to 10' 3-1/2"
4.8",4.8"
Adjustable from 99" to 111" - max 148,Adjustable from 99" to 111" - max 148
我需要输出如下,
output.txt的:
3",3
6-position,
7' 4" to 10' 3-1/2",
4.8",4.8
Adjustable from 99" to 111" - max 148,
所以基本上我需要打印“符号的数字值,其他非数字文本需要被空行代替,awk命令的问题是2,3和5行被删除。
正如我所评论的那样,awk单行程对于您的输入文件没有意义。 不过你可以试试这个sed行:
sed -r '/^[0-9.]+"$/{s/"$//;n;};s/.*//' file
这适用于你的输入文件。
kent$ cat file 3" 6-position 7' 4" to 10' 3-1/2" 4.8" Adjustable from 99" to 111" - max 148 kent$ sed -r '/^[0-9.]+"$/{s/"$//;n;};s/.*//' file 3 4.8 (here, the last line is empty too.)
编辑
删除-r
选项,并调整sed以使用新的输入/输出:
sed 's/^\([^,]*,\)\([0-9.]*\)".*$/\1\2/;t;s/^\([^,]*,\).*/\1/' file
这条线与您的新输入示例一起工作:
kent$ cat file 3",3" 6-position,6-position 7' 4" to 10' 3-1/2",7' 4" to 10' 3-1/2" 4.8",4.8" Adjustable from 99" to 111" - max 148,Adjustable from 99" to 111" - max 148 kent$ sed 's/^\([^,]*,\)\([0-9.]*\)".*$/\1\2/;t;s/^\([^,]*,\).*/\1/' file 3",3 6-position, 7' 4" to 10' 3-1/2", 4.8",4.8 Adjustable from 99" to 111" - max 148,
这适用于我:
sed -e 's/^\([0-9]\+\(\.[0-9]\+\)*\)"$/\1/g' \ -e '/^\([0-9]\+\(\.[0-9]\+\)*\)$/! s/^.*$//g' sample.txt
你可以用awk和sed来做到这一点。 由于我手机上没有Linux(shell),所以我会尝试使用sed解决方案:
sed -e 's/^([0-9.]+)"$|.*/\1/g' < file
应该做这个工作