0

I have the following file:

ICR1 +
ICR1+1+3199 +
ICR1+2526+2828 +
IRT1 +
IRT1+1+1489 +
IRT1+713+937 +
LSR1 -
LSR1+1+1175 -
LSR1+366+638 -
NME1 +
NME1+1+340 +
NME1+2+118 +
PWR1 -
PWR1+1+941 -
PWR1+724+939 -
Q0017 -
Q0017+1+162 -
Q0020 -
Q0020+1370+1513 -
Q0020+1+440 -

The first and second columns are tab-separated. I do need to have the following:

ICR1 +
IRT1 +
LSR1 -
NME1 +
PWR1 -
Q0017 -
Q0020 -

I've tried to use awk with the field separator "+" but it erased the + from the second column as well...

  • 1
    Is your file really structured like that? So on the last line, no tabs around the +? – Panki Jan 16 at 12:17
  • Yes, it's structured like that, there are no tabs after the second column. The first column may be "Name" or "Name+Number" or "Name+Number+Number" – Lucas Farinazzo Marques Jan 16 at 12:18
  • 2
    you want the lines only with "Name" not "Name+something.." ? – Guru Jan 16 at 12:22
  • Yes, that's what I'm looking for – Lucas Farinazzo Marques Jan 16 at 12:31
5

You can set awk's field separator to whitespace or +, and then do the classic associative array based de-duplication:

$ awk -F'[ \t+]' '!seen[$1]++' file
ICR1 +
IRT1 +
LSR1 -
NME1 +
PWR1 -
Q0017 -
Q0020 -
  • Thanks a lot! I didn't know I could do that! Now it'll make my life easier on other similar files as well – Lucas Farinazzo Marques Jan 16 at 12:32
1

Maybe I've misunderstood the problem, but this seems to work:

grep -v '+.' file

Output:

ICR1 +
IRT1 +
LSR1 -
NME1 +
PWR1 -
Q0017 -
Q0020 -
1

I have achieved same by using sed command

sed -n '/^.\{1,5\} .$/p' filename

output

ICR1 +
IRT1 +
LSR1 -
NME1 +
PWR1 -
Q0017 -
Q0020 -
1

Using Miller:

mlr --tsv --implicit-csv-header --headerless-csv-output \
    put -S '$1=gsub($1,"[+].+$","")' then uniq -a inputfile

and the output is:

ICR1    +
IRT1    +
LSR1    -
NME1    +
PWR1    -
Q0017   -
Q0020   -

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.