check if a character is inside a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers check if a character is inside a variable
# 1  
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
Code:
var=1 2 3 4 5 6 7 8 9 0

i would like to see if a newly inputed number is already inside that variable before putting it in.

i hope ive explained myself enough.

p.s. if it is possible to use SED/AWK i cant use those so solutions without using those would be awesome.

thanks in advance
# 2  
Quote:
Originally Posted by strasner
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
Code:
var=1 2 3 4 5 6 7 8 9 0

i would like to see if a newly inputed number is already inside that variable before putting it in.

i hope ive explained myself enough.

p.s. if it is possible to use SED/AWK i cant use those so solutions without using those would be awesome.

thanks in advance
Without knowing what you're trying to do, I'm not sure how helpful this will be:

Code:
(04:52:30+deco@DeCoBuntu)
[~/Desktop]$x=7;for i in 1 2 3 4 5 6 7 8 9;do if [[ -n `echo $i|grep $x` ]];then echo "$i is $x";else echo "$i is not $x";fi;done
1 is not 7
2 is not 7
3 is not 7
4 is not 7
5 is not 7
6 is not 7
7 is 7
8 is not 7
9 is not 7

Basically I'm just saying that for each number in the list, check to see that number matches a predefined number. In retrospect, I suppose it could have just as easily been:

Code:
 (04:52:49+deco@DeCoBuntu)
[~/Desktop]$x=7;for i in 1 2 3 4 5 6 7 8 9;do if [[ $i -eq $x ]];then echo "$i is $x";else echo "$i is not $x";fi;done
1 is not 7
2 is not 7
3 is not 7
4 is not 7
5 is not 7
6 is not 7
7 is 7
8 is not 7
9 is not 7

Or since you you were looking for awk

Code:
 (05:01:30+deco@DeCoBuntu)
[~]$x=7;for i in 1 2 3 4 5 6 7 8 9;do echo "$i"|awk -v k=$i '{ if ( k == 7 ) print $0}';done

I'm sure someone else can come up with better solutions, but this is what I've got at 5AM.
# 3  
mornign forums again.

Yes i am doing some coursework, but im trying to follow the rules as much as i can.

i am currently trying to replace a particular character inside a saved variable with an inputed charater.

so if the saved variable contents were 'book'
and the user entered an 'a', i would like to change only the first 'o' to be an 'a' and leave the second 'o' as it is. so the final outcome would be 'baok'.

so far i have come up with this code which doesnt work.

Code:
replacedletter=$(echo $word | cut -c2 | tr 'o' '$in1')

$in1 being the inputed new letter

any hints would be greatly apreciated by myself.
# 4  
Code:
  echo $word | sed "s/o/$in1/1"

This replaces first occurance of the char 'O'
# 5  
Quote:
Originally Posted by lathavim
Code:
  echo $word | sed "s/o/$in1/1"

This replaces first occurance of the char 'O'
thanks for this solution, however i cant use sed for this. pure bash solutions if possible...
# 6  
Search the forums using our internal Google search engine or our advanced internal search engine.

Here you can find something regarding your question:

https://www.unix.com/shell-programmin...character.html

Regards
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #883
Difficulty: Easy
A computer security vulnerability is a weakness in design, implementation, operation or internal control.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 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

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... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

4. UNIX for Advanced & Expert Users

couting occurences of a character inside a string and assigning it to a variable

echo "hello123" | tr -dc '' | wc -c using this command i can count the no of times a number from 0-9 occurs in the string "hello123" but how do i save this result inside a variable? if i do x= echo "hello123" | tr -dc '' | wc -c that does not work...plz suggest..thanks (3 Replies)
Discussion started by: arindamlive
3 Replies

5. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

6. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

7. AIX

check for a particular character inside a file and substitute with a given character?

i am a newbie to shell script,so i want a kshell script in which i need to check for a particular character inside a file through conditional looping(like if ,case,while)and if that character exists ,then substitute a given character to that character. consider a file test.txt,inside the file... (1 Reply)
Discussion started by: karthikprasathk
1 Replies

8. UNIX for Dummies Questions & Answers

read a variable character by character, substitute characters with something else

im having trouble doing this: i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's? im guessing its like getting the 1's compliment of the string im doing this in... (2 Replies)
Discussion started by: vipervenom25
2 Replies

9. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies

Featured Tech Videos