简体   繁体   English

使用AWK或SED如何删除第一列的字符数不等于13的任何行

[英]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.

相关问题 使用awk(或sed)根据下一行的第一个字符删除换行符 - Using awk (or sed) to remove newlines based on first character of next line 使用AWK或Sed如何在第一个txt之前删除尾随回车符和换行符 - Using AWK or Sed how can I remove trailing carriage return and a line feeds before first txt 使用 awk 或 sed 从 .csv 中的第四列和第五列中删除第一个字符 - Using awk or sed to remove the first character fom the fourt and fifth column in a .csv 如何使用sed或awk打印第一行和段落的模式 - How can I print the first line and the pattern of a paragraph using sed or awk 使用 awk/sed 有条件地删除第 n 列的第一个单词 - Conditionally remove first word of nth column using awk/sed 如何使用sed或awk在文件的特定行插入特定字符? - How to insert a specific character at a specific line of a file using sed or awk? 如何使用 bash/sed 脚本删除文本文件的第一行? - How can I remove the first line of a text file using bash/sed script? 使用sed或awk删除“复选框”字符 - using sed or awk to remove a “checkbox” character 如果该行包含sed / awk或bash的特定单词,同时又保持白色间距,如何更改列中的值? - How can I change a value in a column if that line contains a specific word using sed/awk or bash while keeping the whitespacing? 如何使用“sed 或 awk”从 bash 中的行中删除最后一个逗号 - how to remove last comma from line in bash using "sed or awk"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM