tags:

views:

30

answers:

3

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

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.

nik
not good need uniq fath of the FILE NAME
A: 

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.

Jonathan Leffler
its not good bacuse its also remove the FILE NAME: /dirA/dirB/dirC/dirD/dirE/file line?yael
A: 

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.

Dennis Williamson
please give me example for thatTHX
@yael: Why don't you show what you have so far?
Dennis Williamson