当前位置: 技术问答>linux和unix
请教一个关于Linux shell中文本编辑的问题。
来源: 互联网 发布时间:2016-06-27
本文导语: 我需要实现一个功能。在Linux中 有个文件,大约有上百行,每行以“,”为分隔符,共有8列,我需要取出其中第3,4,7,8列不为零的整行。应该怎么做? 例如说:以下为原始文件。 5555,123456,0,0,555,0,0,0, 5545,123456,0,0,555,0,0,0...
我需要实现一个功能。在Linux中
有个文件,大约有上百行,每行以“,”为分隔符,共有8列,我需要取出其中第3,4,7,8列不为零的整行。应该怎么做?
例如说:以下为原始文件。
5555,123456,0,0,555,0,0,0,
5545,123456,0,0,555,0,0,0,
5525,123456,0,100,555,0,0,0,
5655,122456,0,0,555,0,0,0,
5655,122245,0,0,555,0,0,0,
5655,172456,0,0,555,0,0,0,
5655,152456,0,0,555,0,0,0,
5555,123456,0,100,555,500,10,200,
5555,123456,0,0,555,500,0,0,
5555,123456,0,0,555,0,0,2000,
我需要将以下显示结果提取出来:
5525,123456,0,100,555,0,0,0,
5555,123456,0,100,555,500,10,200,
5555,123456,0,0,555,0,0,2000,
请教各位高手,应该用什么指令能实现这个功能。
有个文件,大约有上百行,每行以“,”为分隔符,共有8列,我需要取出其中第3,4,7,8列不为零的整行。应该怎么做?
例如说:以下为原始文件。
5555,123456,0,0,555,0,0,0,
5545,123456,0,0,555,0,0,0,
5525,123456,0,100,555,0,0,0,
5655,122456,0,0,555,0,0,0,
5655,122245,0,0,555,0,0,0,
5655,172456,0,0,555,0,0,0,
5655,152456,0,0,555,0,0,0,
5555,123456,0,100,555,500,10,200,
5555,123456,0,0,555,500,0,0,
5555,123456,0,0,555,0,0,2000,
我需要将以下显示结果提取出来:
5525,123456,0,100,555,0,0,0,
5555,123456,0,100,555,500,10,200,
5555,123456,0,0,555,0,0,2000,
请教各位高手,应该用什么指令能实现这个功能。
|
$ cat 3478.txt
5555,123456,0,0,555,0,0,0,
5545,123456,0,0,555,0,0,0,
5525,123456,0,100,555,0,0,0,
5655,122456,0,0,555,0,0,0,
5655,122245,0,0,555,0,0,0,
5655,172456,0,0,555,0,0,0,
5655,152456,0,0,555,0,0,0,
5555,123456,0,100,555,500,10,200,
5555,123456,0,0,555,500,0,0,
5555,123456,0,0,555,0,0,2000,
$ awk -F, '$3!~/^0$/ || $4!~/^0$/ || $7!~/^0$/ || $8!~/^0$/ {print $0}' 3478.txt
5525,123456,0,100,555,0,0,0,
5555,123456,0,100,555,500,10,200,
5555,123456,0,0,555,0,0,2000,
5555,123456,0,0,555,0,0,0,
5545,123456,0,0,555,0,0,0,
5525,123456,0,100,555,0,0,0,
5655,122456,0,0,555,0,0,0,
5655,122245,0,0,555,0,0,0,
5655,172456,0,0,555,0,0,0,
5655,152456,0,0,555,0,0,0,
5555,123456,0,100,555,500,10,200,
5555,123456,0,0,555,500,0,0,
5555,123456,0,0,555,0,0,2000,
$ awk -F, '$3!~/^0$/ || $4!~/^0$/ || $7!~/^0$/ || $8!~/^0$/ {print $0}' 3478.txt
5525,123456,0,100,555,0,0,0,
5555,123456,0,100,555,500,10,200,
5555,123456,0,0,555,0,0,2000,
|
用awk就可以了,只要找出第3,4,7,8四列内容拼起来不是"0000"(按文本比较)或0(按数字比较)的行,然后重定向到一个文件即可:
比如:
awk -F, '{if ( $3$4$7$8 != "0000" ) print $0}' 原始文件名 > 目标文件名
或
awk -F, '{if ( $3$4$7$8 != 0 ) print $0}' 原始文件名 > 目标文件名
比如:
awk -F, '{if ( $3$4$7$8 != "0000" ) print $0}' 原始文件名 > 目标文件名
或
awk -F, '{if ( $3$4$7$8 != 0 ) print $0}' 原始文件名 > 目标文件名
|
grep -v "[0-9]*,[0-9]*,0,0,[0-9]*,[0-9]*,0,0," 文件名
grep的条件是这4列全部为0的
-v表示匹配不符合条件的,所以得到的就是你要的结果
grep的条件是这4列全部为0的
-v表示匹配不符合条件的,所以得到的就是你要的结果
|
awk -F',' '{if($3!=0||$4!=0||$7!=0||$8!=0)printf $0"n"}' test.txt
|
awk -F, '$3||$4||$7||$8' URFILE