-3

awk -F ',' '$2 ~ /^ *[0-9]*(\.[0-9]+)?" *$/{sub(/"/, "\n", $2);}' sample.txt > out.txt

hi all,

what's wrong in the above awk command, i'm getting only the empty file. Realted to Need to convert the below awk command into sed

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

expected output is,

output.txt:

3",3
6-position,
7' 4" to 10' 3-1/2",
4.8",4.8
Adjustable from 99" to 111" - max 148,

4

2 回答 2

0

查看数据,您的 awk 语句没有任何“错误”,只是您的正则表达式与您的单个输入不匹配。

于 2013-05-12T00:21:27.723 回答
0

下面的 awk 有什么问题

awk -F ',' '$1 ~ /^ [0-9] (.[0-9]+)?" *$/{sub(/"/, "\n", $1);}' sample.txt > out.txt

直接回答你的问题:

假设正则表达式部分是正确的。你做了替换,但没有打印/输出任何东西。所以你会得到一个空的out.txt

编辑

根据您的示例添加一个有效的 awk 解决方案:

kent$  awk -F, -v OFS="," '{if($2~/^[0-9.]+"$/)sub(/"/,"",$2);else $2=""}1' file
3",3
6-position,
7' 4" to 10' 3-1/2",
4.8",4.8
Adjustable from 99" to 111" - max 148,
于 2013-05-12T00:22:24.680 回答