开发者

awk + remove all not uniq lines except the first line uniq (FILE NAME)

hi I have the following file How to remove by sed all FILE NAME lines except the first uniq FILE NAME For example need to remove all FILE NAME lines from the file except the first:

FILE NAME: /dir1/dir2/dir3/dir4/dir5/file
FILE NAME: /dirA/dirB/dirC/dirD/dirE/file

the file:

FILE NAME: /dir1/dir2/dir3/dir4/dir5/file
PARAMETER NAME: blablabla
TARGET FILE: 12
SOURCE FILE: 开发者_如何学C565
FILE NAME: /dir1/dir2/dir3/dir4/dir5/file
PARAMETER NAME: blablabla
TARGET FILE: 18
SOURCE FILE: 552
FILE NAME: /dir1/dir2/dir3/dir4/dir5/file
PARAMETER NAME: blablabla
TARGET FILE: 14
SOURCE FILE: 559
FILE NAME: /dirA/dirB/dirC/dirD/dirE/file
PARAMETER NAME: blablabla
TARGET FILE: 134
SOURCE FILE: 344
FILE NAME: /dirA/dirB/dirC/dirD/dirE/file
PARAMETER NAME: blablabla
TARGET FILE: 13
SOURCE FILE: 445
FILE NAME: /dirA/dirB/dirC/dirD/dirE/file
PARAMETER NAME: blablabla
TARGET FILE: 13
SOURCE FILE: 434


If you just want the unique file name lines like the two lines you list for the sample you show,

grep "^FILE NAME" input.txt | sort | uniq


Update:
I am not sure what you really want.
the sort | uniq filter will fetch you all unique path names across the lines starting with "^FILE NAME". If your sample input and output is not correct, you might want to correct that with the edit button.


In awk, which the question is tagged with:

awk '$0  ~ /^FILE NAME: / { if (count++ == 0) print; }
     $0 !~ /^FILE NAME: / {                   print; }'

You probably can do it with sed, but it isn't particularly clean.


The question is clarified slightly by the comment. The answer is remarkably similar, though the difference is important:

awk '$0  ~ /^FILE NAME: / { if (count[$0]++ == 0) print; }
     $0 !~ /^FILE NAME: / {                       print; }'

I'm still assuming that you want all the other lines - the example output only shows 'FILE NAME' lines, but the question says nothing about deleting the other lines. Clearly, if you don't want the other lines, eliminate the second line of the awk script.


You can use an associative array in AWK and if a line contains "FILE NAME" check to see if it's in the array. If it is then don't print it. If it isn't then save it in the array and print it. For any lines that don't contain "FILE NAME", print them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜