how can I check the first character of each variable stored in array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how can I check the first character of each variable stored in array
# 1  
Old 10-11-2011
how can I check the first character of each variable stored in array

variables are stored in this format

D0000, D12345, S1234, D12345 | KestrelPrdV2_0.build.177 - svn 141115 tag as V2.0.0.14


D0000, D12345, S1234, D12345

My question is how can I check the first character of each variable stored in array.It should be start with D or S.

Please help

thanks in advance
# 2  
Old 10-11-2011
Did you mean "How to check whether elements in an array starts with D or S"?
# 3  
Old 10-11-2011
expecting this ..
Code:
$ echo "S0000" | nawk '{if($0~/^D/||/^S/) {print $0} else {print "not starts"}}'
S0000
$ echo "D0000" | nawk '{if($0~/^D/||/^S/) {print $0} else {print "not starts"}}'
D0000
$ echo "X0000" | nawk '{if($0~/^D/||/^S/) {print $0} else {print "not starts with D or S"}}'
not starts with D or S

# 4  
Old 10-11-2011
How we can check number of variables in a single statement

A="D0000 D0005 D4300 D1230"
# 5  
Old 10-11-2011
Code:
$ echo $A
D0000 D0005 D4300 D1230 X0000
$ echo $A | tr ' ' '\n' | nawk '{if($0~/^D/||/^S/) {print $0" starts"} else {print $0" not starts"}}'
D0000 starts
D0005 starts
D4300 starts
D1230 starts
X0000 not starts
$

# 6  
Old 10-11-2011
Its really very helpful.Is possible that it should be terminated while the first character is not start with D or S..no need to check further,

Thanks in advance

---------- Post updated at 05:07 AM ---------- Previous update was at 04:59 AM ----------

thanks I am able to get my desired results.
# 7  
Old 10-11-2011
Code:
$ echo $A
D0000 X0000 D0005 D4300
$ echo $A | tr ' ' '\n' | nawk '{if($0!~/^D/||/^S/) {exit} else {print $0}}'
D0000
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

How to check values stored in variable?

hey, i have stored values of query like this val_2=$( sqlplus -s rte/rted1@rel75d1 << EOF set heading off select source_id from cvt_istats where istat_id > $val_1; exit EOF ) echo $val_2 now , val_2 has five values displayed like this 1 2 3 4 5 what i have to do is to check the... (1 Reply)
Discussion started by: ramsavi
1 Replies

2. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

3. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

4. Shell Programming and Scripting

Delete first character from a string stored in a variable

Hallo! Example. #!/bin/bash BACKUP_DIR=/home/userx/backups/evolution echo $BACKUP_DIR # delete the first character from the string BACKUP_DIR=$(echo $BACKUP_DIR | cut -c 2-) echo $BACKUP_DIR It works. It does want I want, delete the first character from string in the... (11 Replies)
Discussion started by: linuxinho
11 Replies

5. Shell Programming and Scripting

How to remove a defined character on an array variable in a do while statement in ksh

I have a korn shell code here on a while do statement which replace the string stored on an array removing double quotes characters on it but it doesn't work. example record: appointmentDate = "tree" which value should result to tree #!/bin/ksh # Remove " on string records let recordCount=3... (5 Replies)
Discussion started by: ryukishin_17
5 Replies

6. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

7. UNIX for Dummies Questions & Answers

check if a character is inside a variable

Good morning forums. Ive been trying all weekend to do this with no luck, so i have come to the forums for help. What i want to do is check if an inputed character is inside a variable. for example, if var=1 2 3 4 5 6 7 8 9 0 i would like to see if a newly inputed number is already... (5 Replies)
Discussion started by: strasner
5 Replies

8. UNIX for Advanced & Expert Users

Escaping special character stored in variables : perl

Hi just for regular use i m working on small module written in perl for getting date in specified format like i have to specify date format and then seperator to seperate date i am 95% done. now i m sure explanation i gave is not good enough so i am putting output here : C:\Documents and... (2 Replies)
Discussion started by: zedex
2 Replies

9. Shell Programming and Scripting

How to check the variable is present in array or not ?

Hi , I am trying to check wether the variable is present in the array. please see the below code .when ever i do this its taking only the first value of the array . please advise. ###Code Snnipet ### #!/bin/ksh set -xv if ]; then echo " you have Specified the ORG ID - $1 " ... (1 Reply)
Discussion started by: padhu.47
1 Replies

10. Shell Programming and Scripting

Prase a file and stored the result to an array

Dear all , I have a file whose content has the following format: jboss.web:type=ThreadPool,name=AAAA jboss.web:type=ThreadPool,name=BBBB How can I parse this file to get the value of the name of each line (AAAA , BBBB) and store the result to an array ? (array = AAAA , array = BBBB).... (4 Replies)
Discussion started by: youareapkman
4 Replies
Login or Register to Ask a Question