Get first four characters from the variable name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get first four characters from the variable name
# 1  
Old 08-13-2014
Get first four characters from the variable name

Hi all

I have a variable name as below

Code:
runbatch=FCSTworLOADfx_CYR

Now i need to search for FCST in this variable and run a program

I am doing like this but it is failing

Code:
if [${RUNBATCH} |grep "FCST" == "FCST" ]; then ****RUN PROGRAM**

Please help

DJ

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code samples.

Last edited by Don Cragun; 08-13-2014 at 05:53 AM.. Reason: Add CODE tags.
# 2  
Old 08-13-2014
Code:
if [[ "$(echo $runbatch | grep FCST)" != "" ]]; then echo "find";fi

This User Gave Thanks to protocomm For This Post:
# 3  
Old 08-13-2014
Code:
if [[ $(grep "FCST" <<<${runbatch}) ]]; then ***RNU PROGRAM***

This User Gave Thanks to SriniShoo For This Post:
# 4  
Old 08-13-2014
Quote:
Originally Posted by Hypesslearner
Hi all

I have a variable name as below

Code:
runbatch=FCSTworLOADfx_CYR

Now i need to search for FCST in this variable and run a program

I am doing like this but it is failing

Code:
if [${RUNBATCH} |grep "FCST" == "FCST" ]; then ****RUN PROGRAM**

Please help

DJ

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code samples.
This can all be done with standard variable expansions without needing to invoke utilities that aren't built into the shell:
Code:
if [ "${runbatch#*FCST}" != "$runbatch" ]
then    ****Run Program***
fi

You said run the program if FCST appears in $runbatch. If you meant if FCST appears as the 1st four characters in $runbatch, change "${runbatch#*FCST}" to "${runbatch#FCST}".

Note that spaces around [ and ] are crucial and shell variable names are case sensitive.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Escape characters in a variable

Debian 9 64x - LXDE How can i disable escape sequences in a variable? #!/bin/bash #mainscript . "./links.bash" echo "$red_start This text should be red $color_end"#!/bin/bash #links.bash #colors red_start="\eOutput that i get: \e Output expected: This text should be... (5 Replies)
Discussion started by: int3g3r
5 Replies

2. Shell Programming and Scripting

[Solved] Count characters of variable from right

My variable is something like: f="/Volumes/VERVE/MOOTON_CALL/01_shots/XX/xx0195/Projects/program/rs0195_v400001.aep" I use ${f:63:6} to call "rs0195" as characters counted from the left, but it'd be so much easier to count from the right. If ${f:95:10} counts from the left, what would... (2 Replies)
Discussion started by: scribling
2 Replies

3. Shell Programming and Scripting

Unset variable with characters in value

I have a script with a $PASSWORD variable. I unset it right after using it, just to minimize the chance it could be left around for a snooper. That worked just fine... until I used a password with a value of "P@ssw0rd" Now, unset (even with -f, even with the variable enquoted) tells me: unset:... (1 Reply)
Discussion started by: jnojr
1 Replies

4. Shell Programming and Scripting

Variable being stored with additional characters

I am trying to store a string value to a temporary variable within my script. When I run the command manually it returns the expected value, but from the script an additional "\r" is at the end. tCurrentStatus=`cat ${tPC_status} | grep 'Current status' | awk '{print $NF}'` output: cat... (1 Reply)
Discussion started by: jxrst
1 Replies

5. Shell Programming and Scripting

getting first two characters of variable

From a shell script, I'm trying to get the first two characters of an environment variable. If I type at the command promot: XX=`echo $MYVAR | cut -c1-2` echo $XX It works just fine However, if I execute the exact same thing from a shell script, I get: cut: you must specify a list of... (2 Replies)
Discussion started by: Boomn4x4
2 Replies

6. Shell Programming and Scripting

Cut last 7 characters of a variable

I need to cut the last seven characters of a variable length variable. The variable may be 7 characters or 70. I need to always be able to grab the last 7 characters. I looked at the cut command but it always seems to want to start at the beginning of a line, not the end and count backwards. ... (5 Replies)
Discussion started by: kblawson14
5 Replies

7. Shell Programming and Scripting

Stripping characters from a variable

I'm using a shell script to get user input with this command: read UserInput I would then like to take the "UserInput" variable and strip out all of the following characters, regardless of where they appear in the variable or how many occurrences there are: \/":|<>+=;,?*@ I'm not sure... (5 Replies)
Discussion started by: nrogers64
5 Replies

8. Shell Programming and Scripting

Cutting characters in a variable

I have a variable var which contains "ABCDEFDH" Now I have to remove the first 4 characters that is "ABCD" so my variable should contain only "DEFH" plzz tell me how to do that . i am using bash shell (1 Reply)
Discussion started by: cynosure2009
1 Replies

9. UNIX for Dummies Questions & Answers

extract characters from a variable

Hi All, I have scenario as below, I want to cut the characters and store it variable2.. and want to display the variable2.. as shown below... eg: the size may differs , i am show just an example..... numbers are not fixed in variable1..i want to extract characters and... (1 Reply)
Discussion started by: G.K.K
1 Replies

10. UNIX for Dummies Questions & Answers

To get the characters from a variable

Hi , I need to get the specified word from the variable.can some one help me out in this ? input=ASD34567P i need to get the value as : output1=34567 So i need to ignore the last character and get the remaining values. THANKS IN ADVANCE. (6 Replies)
Discussion started by: ithirak17
6 Replies
Login or Register to Ask a Question