[英]Using AWK or SED how can I remove any line where the first column's character count doesn't equal 13
Using AWK or sed how can I remove any line where the timestamp (first column) is not equal to 13 numeric characters while ignoring the first line. 使用AWK或sed如何删除时间戳(第一列)不等于13个数字字符而忽略第一行的任何行。
Before: 之前:
timestamp,pageNo,description
1451317591621,01,Home Page Request
14513,Home Page Request
1451317591623,03,Home Page Request
1451317,04,Home Page Request
1451317591625,05,Home Page Request
After: 后:
timestamp,pageNo,description
1451317591621,01,Home Page Request
1451317591623,03,Home Page Request
1451317591625,05,Home Page Request
Using sed
, pass if the line number is one or the first field consists of exactly thirteen digits; 使用
sed
,如果行号为1则传递,或者第一个字段由十三个数字组成; else, delete. 否则,删除。
sed -r -e '1b' -e '/^[0-9]{13},/b' -e d file
Using Awk, similarly, print if line number is one or the first field is thirteen characters and all numbers. 使用Awk,类似地,如果行号为1则打印,或者第一个字段是十三个字符和所有数字。
awk -F , 'NR == 1 || (len($1) == 13 && $1 ~ /^[0-9]*$/)' file
Using awk
(requires gawk 4+ or 3+ with --re-interval option) 使用
awk
(需要gawk 4+或3+ with --re-interval选项)
awk -F, '$1~/^[0-9]{13}$/||NR==1' file
Using sed
使用
sed
sed '/^[0-9]\{13\},/p;1p;d' file
awk -F, 'NR==1 || (length($1) == 13 && $1+0 == $1)' file
If Perl is an option: 如果Perl是一个选项:
perl -F, -ane 'print if $F[0] =~ /^[0-9]{13}$/ or $. == 1' file
These command-line options are used: 使用这些命令行选项:
-n
loop around each line of the input file -n
循环输入文件的每一行 -a
autosplit mode – split input lines into the @F
array. -a
autosplit模式 - 将输入行拆分为@F
数组。 Defaults to splitting on whitespace. -e
execute the perl code -e
执行perl代码 -F
autosplit modifier, in this case splits on ,
-F
autosplit修饰符,在这种情况下拆分,
$.
is the line number 是行号
@F
is the array of words in each line, indexed starting with $F[0]
@F
是每行中的单词数组,以$F[0]
开头索引$F[0]
output: 输出:
timestamp,pageNo,description
1451317591621,01,Home Page Request
1451317591623,03,Home Page Request
1451317591625,05,Home Page Request
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.