Question: .bash_profile shortcut to reverse a string
0
gravatar for johan
5 weeks ago by
johan50
France
johan50 wrote:

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 • 115 views
ADD COMMENTlink modified 5 weeks ago by ATpoint21k • written 5 weeks ago by johan50
3
gravatar for ATpoint
5 weeks ago by
ATpoint21k
Germany
ATpoint21k wrote:

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 COMMENTlink modified 5 weeks ago • written 5 weeks ago by ATpoint21k

Thanks for the quick reply! Works perfectly.

ADD REPLYlink written 5 weeks ago by johan50
1
gravatar for rizoic
5 weeks ago by
rizoic200
rizoic200 wrote:

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 COMMENTlink modified 5 weeks ago by RamRS23k • written 5 weeks ago by rizoic200
1

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 REPLYlink written 5 weeks ago by RamRS23k

Thanks for the quick reply! Works perfectly.

ADD REPLYlink written 5 weeks ago by johan50
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 959 users visited in the last hour