我想删除第二列是05408736032的所有行。
0009300 | 05408736032 | 89 | 01 | 001 | 0 | 0 | 0 | 1 | NNNNNNYNNNNNNNNN | ASDF | 0009367 | 05408736032 | 89 | 01 | 001 | 0 | 0 | 0 | 1 | NNNNNNYNNNNNNNNN | adff |
这可能做你想做的事情:
sort -t '|' -k 2,2 -u foo.dat
然而,这根据你的领域,你可能不想要的inputsorting。 如果你真的只想删除重复,你最好的select是Perl:
perl -ne '$a=(split "\\|")[1]; next if $h{$a}++; print;' foo.dat
awk -F \| '{if ($2 != 05408736032) print}'
纯粹的Bash:
oldIFS=$IFS while read line do IFS=$'|' testline=($line) # make an array split according to $IFS IFS=$oldIFS # put it back as soon as you can or you'll be sooOOoorry if [[ ${testline[1]} != "05408736032" ]] then echo $line fi done < datafile
你可以做一些事情:
for f in `cat $file`; do val=`echo $f | cut -d\| -f 2` if [ `grep $val $file | wc -l` -lt 2 ]; then echo $f fi done
但是,像大多数shell脚本一样,效率相当低。 你最好在Perl中做这件事,就像:
@infile=<>; foreach (@infile) { @foo = split(/|/); if exists $found{$foo[1]} { $found{$foo[1]}++; } else { $found{$foo[1]}++; } } foreach (@infile) { @foo = split(/|/); if ($found{$foo[1]} < 2) { print $_; } }
是否要删除第二个|的所有行? 分隔的字段包含'05408736032'? 所有的行会被格式化吗? 如果是这样,这应该输出文件减去这些行(这是perl,将原始文件作为第一个参数,文件将作为第二个)。
#!/usr/bin/perl use warnings; use strict; my ($file1, $file2) = @ARGV; open my $origin_file, '<', $file1; open my $newfile, '>', $file2; while (my $line = <$origin_file>) { my @values = split '/|/', $line; print $newfile $line unless $vaules[1] = '05408736032'; } close $newfile or die $!; close $origin_file or die $!;
(我没有testing过这个,所以你可能想在尝试之前备份原始文件)
在再次阅读时,您可能正在寻找仅抓取具有唯一第二列的行。 这应该做到这一点。
#!/usr/bin/perl use warnings; use strict; my ($file1, $file2) = @ARGV; open my $origin_file, '<', $file1; open my $newfile, '>', $file2; while (my $line = <$origin_file>) { my @values = split '/|/', $line; print $newfile $line unless defined $unique{$values[1]}; $unique{$vaules[1]} += 1; } close $newfile or die $!; close $origin_file or die $!;