如果定界符的数量不等于 header,则删除一行
Remove a line if count of delimiter is not equal to header
如果 DELIMITER 字符的计数不等于 header 计数,我想编写一个实用程序从 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.
来做就好了
试试这个:
awk -F\| 'NR==1{n=NF}NF==n' File
设置|
作为分隔符。将第一行 (NR==1
) 中出现的字段数 (NF
) 保存到变量 n
,然后只打印字段数等于 n
的行 (NF==n
).
样本:
$ cat File
NAME|ADDRESS|PHONE
Jack|SA|123|aaa|aaaa
JOHN|New York|111-222-3333
Jane|New Jer
$ awk -F\| 'NR==1{n=NF}NF==n' File
NAME|ADDRESS|PHONE
JOHN|New York|111-222-3333
这里是一个例子(没有测试所以可能会存在一些语法错误,但你应该有一个大概的想法)
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)
通过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
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)
如果 DELIMITER 字符的计数不等于 header 计数,我想编写一个实用程序从 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.
来做就好了试试这个:
awk -F\| 'NR==1{n=NF}NF==n' File
设置|
作为分隔符。将第一行 (NR==1
) 中出现的字段数 (NF
) 保存到变量 n
,然后只打印字段数等于 n
的行 (NF==n
).
样本:
$ cat File
NAME|ADDRESS|PHONE
Jack|SA|123|aaa|aaaa
JOHN|New York|111-222-3333
Jane|New Jer
$ awk -F\| 'NR==1{n=NF}NF==n' File
NAME|ADDRESS|PHONE
JOHN|New York|111-222-3333
这里是一个例子(没有测试所以可能会存在一些语法错误,但你应该有一个大概的想法)
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)
通过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
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)