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: 565
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

Answers


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.


Need Your Help

MySQL - Search PHP script

php mysql

I'm currently coding my own CMS and I'm at the point of the search.php. I need to search in 4 tables that haves different columns name within only one query because my server is not the fastest.

Looping and ignoring flag - Python

python loops

I am working on learning python and decided to write a small battle engine to use a few of the different items I have learned to make something a bit more complicated. The problem I am having is th...