0

This is the text that have in the file:

                   1NPD:A|PDBID|CHAIN|SEQUENCE  MDVTAKYELIGLMAYPIRHSLSPEMQNKALEKAGLPFTYMAFEVDNDSFPGAIEGLKALKMRGTGVSMPNKQLACEYV
                   1NPD:B|PDBID|CHAIN|SEQUENCE  MDVTAKYELIGLMAYPIRHSLSPEMQNKALEKAGLPFTYMAFEVDNDSFPGAIEGLKALKMRGTGVSMPNKQLACEYV
                   1NPY:A|PDBID|CHAIN|SEQUENCE  MINKDTQLCMSLSGRPSNFGTTFHNYLYDKLGLNFIYKAFTTQDIEHAIKGVRALGIRGCAVSMPFKETCMPFLDEIH
                   1NPY:B|PDBID|CHAIN|SEQUENCE  MINKDTQLCMSLSGRPSNFGTTFHNYLYDKLGLNFIYKAFTTQDIEHAIKGVRALGIRGCAVSMPFKETCMPFLDEIH                       
                   1NPY:C|PDBID|CHAIN|SEQUENCE  MINKDTQLCMSLSGRPSNFGTTFHNYLYDKLGLNFIYKAFTTQDIEHAIKGVRALGIRGCAVSMPFKETCMPFLDEIH                       
                   1NPY:D|PDBID|CHAIN|SEQUENCE  MINKDTQLCMSLSGRPSNFGTTFHNYLYDKLGLNFIYKAFTTQDIEHAIKGVRALGIRGCAVSMPFKETCMPFLDEIH                       
                   1NVT:A|PDBID|CHAIN|SEQUENCE  GPLGSMINAKTKVIGLIGHPVEHSFSPIMHNAAFKDKGLNYVYVAFDVLPENLKYVIDGAKALGIVGFNVTIPHKIEI                       
                   1NVT:B|PDBID|CHAIN|SEQUENCE  GPLGSMINAKTKVIGLIGHPVEHSFSPIMHNAAFKDKGLNYVYVAFDVLPENLKYVIDGAKALGIVGFNVTIPHKIEI

I want to remove the spaces that it has in the beginning, I think there is a tab as well, and I only want to keep the ID which is 1NPD, 1NPY, etc. It doesn't matter if it is A, B or C, I just want one ID of each sequence.

This is how I would like the file to look.

1NPD
1NPY
1NVT
2

Assuming you have the text you want to process in the file in.file and you want to write to a file out.file, you can use:

tr -d ' ' < in.file | cut -d':' -f1 | sort -u > out.file

The first, removes the spaces from in.file: tr -d ' ' < in.file

The second, cuts the text into fields using ':' as the delimiter and selects only the first field: cut -d':' -f1

The last, sorts the results, selects the uniques and writes to out.file: sort -u > out.file

| improve this answer | |
1

With just awk

awk -F: '{sub("[ \t]+","",$1)}; !seen[$1]++ {print $1}' textfile
  • split the text into :-delimited fields
  • remove leading whitespace from the first field $1
  • print the trimmed $1 only the first time you see it
| improve this answer | |
1

Here's a slight simplification of @archemar's answer

awk -F'( *|:)' '{print $2}' test.tmp | sort -u

The -F sets the field separator to a given regular expression, in this case a :, or any number of spaces (which makes the leading set of spaces a field separator). This puts the desired values in column 2.

As in other answers, sort -u sorts and ensures uniqueness.

| improve this answer | |
0

Just do it with following shell commands: sed 's/^[\t ]*//g' /tmp/data|awk -F : '{print $1}'|uniq|tr '\n' ' ' Remove the tabs and space at beginning, select the first column by awk, then merge the same IDs, using tr to convert multi-lines to one line.

| improve this answer | |
0

in a single awk command

 awk '{print substr($1,1,index($1,":")-1)}' in.file | sort -u

where

  • awk will trim withe space and tab
  • index($1,":") fetch : position
  • substr( ) extract substring
  • sort -u will ensore uniqueness
| improve this answer | |
0

With a grep implementation with PCRE support:

pcregrep -o '^\s*\K[^:]*'

Some grep implementations have -P for PCRE matching, and -o for outputting the matched part of the line, though neither are standard options:

grep -P '^\s*\K[^:]*'

The \s* skips the leading (^) whitespace, and \K sets the start of the matched part after it.

| improve this answer | |
0

Another way to do it with sed. This one uses a backreference in order to replace the input-line with just the id we're interested in:

$ sed 's/^\s*\(.*\):.*/\1/' your_file | sort -u
1NPD
1NPY
1NVT

We are replacing...

  • ^\s* any amount of whitespace at the beginning of the line,
  • \(.*\) followed by anything (this is \1 ) that gets followed by...
  • :.* a colon and then anything.

...with \1 which is what is between the whitespace and the colon.

| improve this answer | |

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.