shell - Condition on Nth character of string in a Mth column in bash
问题描述
I have a sample
$ cat c.csv
a,1234543,c
b,1231456,d
c,1230654,e
I need to grep only numbers where 4th character of 2nd column but not be 0 or 1
Output must be
a,1234543,c
I know this only
awk -F, 'BEGIN { OFS = FS } $2 ~/^[2-9]/' c.csv
Is it possible to put a condition on 4th character?
解决方案
请您尝试以下操作。
awk 'BEGIN{FS=","} substr($2,4,1)!=0 && substr($2,4,1)!=1' Input_file
或根据 Ed 网站的建议:
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.
推荐阅读
- python - 熊猫 slice_replace 与系列
- mysql - 有没有办法在 MySQL 中重新排序相关表?
- reactjs - 悬停时的 React-Bootstrap 下拉菜单
- c# - 无法从脚本中统一找到按钮
- spring - 我不明白 prePostEnabled 的含义/用法
- python - Google Calendar API 循环事件,EXDate 不起作用
- javascript - 使用 Javascript 向表中添加新行
- typescript - NestJS:如何将日期对象转换为自定义格式(自定义字符串或 unix 时间戳)
- io - 如何在 Julia 分布式 for 循环中以非交互方式运行 I/O?
- wordpress - Woo-commerce - 添加产品 - 列表页面 - 设计未正确显示 - Wordpress