LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2011, 10:37 PM   #1
piman
LQ Newbie
 
Registered: Aug 2011
Location: DC
Posts: 5

Rep: Reputation: Disabled
bash script similar to javascript var.substring(start,end)


I am looking for a way to verify the first the characters of an input and once verified continue taking input every three chars. My javascript code is: if (RNA.substring(0,3) != 'AUG') {

alert('You did not start with the first AUG codon!');

}



if (RNA.substring(0,3) == 'AUG') {

result.push('M/Start');

}



for (i=0;i<=100;i++) {

if (RNA.substring(start,stop) == '"B"') {

result.push('B');

var start = start+3;

var stop = stop+3;

}
so I need a similar way to do this in bash. I know how to do a for loop but I do not know how to take input in three characters at a time.
Any help is appreciated as I am pretty new to Bash.
Thanks in advance!
 
Old 08-29-2011, 12:15 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
Using this page as a guide http://www.cyberciti.biz/faq/linux-u...me-while-loop/ you can probably make something work, if you have your input string in a file on the file system.


Code:
[user@vm:~/testing]$ cat INPUT
ABCDEFGHIJKLMNOPQRSTUVWXYZ


[user@vm:~/testing]$ cat arraytest.bash 
#!/bin/bash

while IFS= read -r -n3 char
do
echo $char
done < "./INPUT"


[user@vm:~/testing]$ ./arraytest.bash 
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
YZ
 
Old 08-29-2011, 07:54 PM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
With bash, you can do index slicing as well, eg

Code:
${string:0:3}
 
Old 09-02-2011, 09:17 PM   #4
piman
LQ Newbie
 
Registered: Aug 2011
Location: DC
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks both!

Thank you both for your answers. kurumi's answer looks easier, but I had already rewritten the script with thesnow's answer and I do not want to redo it again. If anyone cares the full script will be posting shortly at http://mikedombrowski.com/mrna.bash
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script to categorize folders on similar names GnuBoi Linux - General 1 10-10-2010 07:25 AM
[SOLVED] substring in bash script j1alu Linux - Newbie 6 07-21-2010 11:23 PM
using awk substring function on a file in a bash script matt007 Programming 3 06-17-2008 08:17 PM
How to make a bash script to start & end apache and mysql ? rova Programming 2 03-01-2008 07:27 PM
/var/log/httpd/error_log:Premature end of script headers! katana Linux - General 0 08-14-2001 06:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration