如果分隔符的数量不等于标题,则删除一行

时间:2015-02-25 11:00:24

标签: python shell awk text-processing

如果DELIMITER字符的数量不等于标题计数,我想编写一个实用程序来删除DELIMITED文件中的行。

例如,如果文件是这样的

NAME|ADDRESS|PHONE
Jack|SA|123|aaa|aaaa
JOHN|New York|111-222-3333
Jane|New Jer

我想要低于输出

NAME|ADDRESS|PHONE
JOHN|New York|111-222-3333

任何人都可以帮助我如何在shell脚本中实现这一点?如果我能用python做它会很棒。

4 个答案:

答案 0 :(得分:4)

试试这个:

awk -F\| 'NR==1{n=NF}NF==n' File

|设置为分隔符。将第一行(NF)中存在的字段数(NR==1)保存到变量n,然后仅打印字段数等于n的行({ {1}})。

<强>示例:

NF==n

答案 1 :(得分:1)

通过python3

with open('file') as f:
    c = f.readline().count('|')      # read the first line and count the occurance of | symbol and store the count to the variable c
    f.seek(0)                        # get back to the top
    for line in f:                   # iterate over all the lines present in the file.
        if line.count('|') == c:     # count the occurances of | on that particular line and check the count against the variable c
            print(line, end="")      # if both counts are equal then print the corresponding line.

<强>输出:

NAME|ADDRESS|PHONE
JOHN|New York|111-222-3333

答案 2 :(得分:0)

这是一个例子(没有测试它,所以一些语法错误可能仍然存在,但你应该得到粗略的想法)

 delimiter='|'
 with open(input,'r') as fr:
   with open(output,'w') as fw:
     headers=fr.readline()
     headersCount=len(headers.split(delimiter))
     fw.write(headers)
     for line in fr.readlines():
       if len(line.split(delimiter))==headersCount:
         fw.write(line)

答案 3 :(得分:0)

Logic is good, syntax and error checking may need work.  Double check

delimiter = '|'
lines = open(file,"r").readlines()
header = lines.pop(0)
print(header)
headerElements = header.count(delimiter)
for data in lines:
  if data.count(delimiter) == headerElements:
    print(data)