2

I need your urgent help and guidance, I have a file in which there is 1000s of sequences , from which I want to cleave K and R and paste the remaining sequences line by line. File A:

MSTNPKPQRK  TKRNTNRRPE  DVKFPGGQI

Desired Output:

MSTNP
PQ
T
NTN
PE
DV
FPGGQI

After getting this output I want to search individual sequences in file B (database of sequences)

1
  • This seems like a very good question for [Code Golf](codegolf.stackexchange.com). Do you mind if I ask this on code golf? @fia
    – user689314
    Jun 16, 2017 at 9:16

3 Answers 3

2

With sed:

sed 's/[KR[:blank:]]\+/\n/g' A.txt
  • sed 's/[KR[:blank:]]\+/\n/g' A.txt substitute all one or more K, R, and space/tab with newline from A.txt

With awk:

awk -F '[KR[:blank:]]+' '{for(i=1; i<=NF; i++) print $i}' A.txt
  • -F '[KR[:blank:]]+' sets the field separator as one or more K, R, space/tab

  • {for(i=1; i<=NF; i++) print $i} iterates over and prints the fields


You can leverage awk for searching resulting patterns on another file by using the two files as arguments, and working from there.

More generic would be to use grep to take patterns from an input filename, you can use process substitution to get a file descriptor:

grep -f <(awk/sed ... A.txt) B.txt

For exact match, add -F:

grep -Ff <(awk/sed ... A.txt) B.txt

Example:

% cat A.txt 
MSTNPKPQRK  TKRNTNRRPE  DVKFPGGQI

% sed 's/[KR[:blank:]]\+/\n/g' A.txt
MSTNP
PQ
T
NTN
PE
DV
FPGGQI

% awk -F '[KR[:blank:]]+' '{for(i=1; i<=NF; i++) print $i}' A.txt 
MSTNP
PQ
T
NTN
PE
DV
FPGGQI
2
  • Dear heemayl,I really appreciate your spontaneous reply can you do me one more favor what if I want to print these with K,R likewise: MSTNPK PQRK TKR NTNRR
    – Rhea
    Jun 16, 2017 at 5:28
  • @fia Here: sed -E 's/[KR[:blank:]]+/&\n/g' A.txt
    – heemayl
    Jun 16, 2017 at 5:33
0

Obtaining desired output can be done in several different ways. Perl and Awk presented below work in exactly same manner. You can redirect either command using > operator to file and then use grep -f pattern.txt data.txt

For instance, with perl presented below, we can do:

 perl -ne '@s=split /[KR ]/,$_; foreach(@s){ print "$_\n" if !/^$/}'  input.txt  > pattern.txt && grep -f pattern.txt data.txt 

Perl

The perl approach is effectivelly the same as AWK explained below: split line into columns using K,R, and space as separators then iterate over all of them printing only non-empty columns.

$ perl -ne '@s=split /[KR ]/,$_; foreach(@s){ print "$_\n" if !/^$/}'  input.txt          
MSTNP
PQ
T
NTN
PE
DV
FPGGQI

AWK

$ awk -F '[KR ]' 'BEGIN{OFS="\n"};{$1=$1;print}' input.txt  | awk '!/^$/'                              
MSTNP
PQ
T
NTN
PE
DV
FPGGQI

Key points of how this works:

  • -F allows us to use space,K and R as separators for columns(aka fields).
  • BEGIN{OFS="\n"} allows us to use newline as output separator so that the items come out separated by newline
  • To apply the new OFS we trigger rebuilding of text line via $1=$1 assignment, and finally print the line.
  • due to multiple column separators K,R, and space being adjacent (for example in PQRK) we have to filter out empty lines from the first awk, which is what the second awk is doing.
0

Replace K,R, and whitespace characters with newlines, squeezing any multiple instances:

$ tr -s '[KR[:space:]]' '\n' < file
MSTNP
PQ
T
NTN
PE
DV
FPGGQI

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .