.bash_profile shortcut to reverse a string
2
0
Entering edit mode
5.4 years ago
johan ▴ 120

I found this little code which I put in a .bash_profile file

lenFunc() {
  echo ${#1}
}

alias len=lenFunc

Typing actg in terminal outputs 4.

I then learned that echo actg|rev outputs gtca. However, I have troubles creating an alias from it.

Neither of these two works for me:

alias revFunc() {
 echo ${#1}|rev
}

alias rev=renFunc

and

alias rev='echo "$1"|rev'

Help is appreciated!

(bonus: if anyone knows a way I can upgrade and also have a "revcom" command for reverse complementary I'll give you a virtual hug)

I run macOS if it matters.

bash terminal • 1.3k views
ADD COMMENT
3
Entering edit mode
5.4 years ago
ATpoint 85k

Why did you put an alias before the function declaration?

It should be

revFunc() {
  echo ${1} | rev 
}

## => also removed the # inside {1} as yu want to print rather than count the input.

alias REV=revFunc

## gives:
REV 1234
> 4321

Also, avoid alias names that are identical to existing system commands like rev, use REV, myrev or similar instead. There was also a typo: alias rev=renFunc was re=>N<=Func instead of re=>V<=Func

For reverse complements, see e.g. fasta - reverse complement sequence or a super-simple https://bioinformatics.stackexchange.com/questions/7458/what-is-a-quick-way-to-find-the-reverse-complement-in-bash which would then be

revComp() {
  echo $1 | tr ACGTacgt TGCAtgca | rev 
}
revComp AAAACCTG
>CAGGTTTT
ADD COMMENT
0
Entering edit mode

Thanks for the quick reply! Works perfectly.

ADD REPLY
1
Entering edit mode
5.4 years ago
rizoic ▴ 250

You just need to remove the alias in your function and change the first argument to $1 as you are not interested in the length i.e. $#1

revFunc() {
      echo ${1}|rev
}

For reverse compliment you can change the function to be

revComp() {
      echo ${1}|tr "[ATGCatgc]" "[TACGtacg]"|rev
}
ADD COMMENT
1
Entering edit mode

Hi rizoic,

Our editor does not support language specification in markdown code blocks yet, which is why your code block was not exposed to syntax highlighting. I have removed the language specification for now. Thank you for exposing this bug!

ADD REPLY
0
Entering edit mode

Thanks for the quick reply! Works perfectly.

ADD REPLY

Login before adding your answer.

Traffic: 1923 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6