我有一个样本
$ cat c.csv
a,1234543,c
b,1231456,d
c,1230654,e我只需要在第二列的第四个字符而不是0或1的情况下进行grep。
输出必须是
a,1234543,c我只知道
awk -F, 'BEGIN { OFS = FS } $2 ~/^[2-9]/' c.csv有可能在第四个角色上加上条件吗?
发布于 2019-12-29 18:48:42
你能试一下吗。
awk 'BEGIN{FS=","} substr($2,4,1)!=0 && substr($2,4,1)!=1' Input_file或根据教育署网站的建议:
awk 'BEGIN{FS=","} substr($2,4,1)!~[01]' Input_file解释:在这里添加了对上述代码的详细说明。
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section from here.
FS="," ##Setting field separator as comma here.
} ##Closing BLOCK for this program BEGIN section.
substr($2,4,1)!=0 && substr($2,4,1)!=1 ##Checking conditions if 4th character of current line is NOT 0 and 1 then print the current line.
' Input_file ##Mentioning Input_file name here.发布于 2019-12-29 23:28:55
这可能适用于您(GNU或grep):
grep -vE '^([^,]*,){1}[^,]{3}[01]' file或者:
sed -E '/^([^,]*,){1}[^,]{3}[01]/d' file 替换该列中m‘-1列的1和n’-1字符的3。
发布于 2019-12-30 15:04:53
Grep是答案。但是下面是另一种使用数组和变量替换的方法
test=( $(cat c.csv) ) # load c.csv data to an array
echo ${test[@]//*,???[0-1]*/} # print all items from an array,
# but remove the ones that correspond to this regex *,???[0-1]*
# so 'b,1231456,d' and 'c,1230654,e' from example will be removed
# and only 'a,1234543,c' will be printedhttps://stackoverflow.com/questions/59522304
复制相似问题