Question: .bash_profile shortcut to reverse a string
0
gravatar for johan
11 months ago by
johan90
Sweden
johan90 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 • 265 views
ADD COMMENTlink modified 11 months ago by ATpoint36k • written 11 months ago by johan90
3
gravatar for ATpoint
11 months ago by
ATpoint36k
Germany
ATpoint36k 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 11 months ago • written 11 months ago by ATpoint36k

Thanks for the quick reply! Works perfectly.

ADD REPLYlink written 11 months ago by johan90
1
gravatar for rizoic
11 months ago by
rizoic230
rizoic230 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 11 months ago by RamRS27k • written 11 months ago by rizoic230
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 11 months ago by RamRS27k

Thanks for the quick reply! Works perfectly.

ADD REPLYlink written 11 months ago by johan90
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: 1097 users visited in the last hour