Visit Our UNIX and Linux User Community


Find string in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find string in variable
# 1  
Old 04-22-2013
Display Find string in variable

korn shell,

testing if a pattern exist in a string,
x is constant, y could be none,all or any combination of the elements of x,

Code:
x="aa bb cc dd ee ff gg hh ii jj kk ll"
y="ff kk"
for z in `echo ${x}`
do
 if [ <any pattern in $y matches any pattern in $x> ]
   then
     do something
 fi
done


Last edited by Scrutinizer; 04-22-2013 at 07:07 PM.. Reason: code tags
# 2  
Old 04-22-2013
Try:
Code:
for z in $x
do
  case $y in
    *$z*) echo "a pattern in \$y matches any pattern in \$x"; break
  esac
done

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-22-2013
Recent kshs (as well as bashes) provide a regex match in conditional expressions. man ksh:
Quote:
Conditional Expressions.
A conditional expression is used with the [[ compound command to test attributes of files and to compare strings. Field splitting and file name generation are not performed on the words between [[ and ]]. Each expression can be constructed from one or more of the following unary or binary expressions: . . .
string =~ ere
True if string matches the pattern ~(E)ere where ere is an extended regular expression.
So your code could read:
Code:
x="aa bb cc dd ee ff gg hh ii jj kk ll"
y="ff kk"
$ for z in $x; do [[ "$y" =~ "$z" ]] && echo $z; done
ff
kk

Aside: that `echo ${x}` is pointless - get rid of it.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-22-2013
If 'pattern' stands for 'word' and 'match' for 'is equal to', then nested loops seem most correct.
Code:
for x1 in $x; do
 for y1 in $y; do
  if [ "$x1" = "$y1" ]; then
   echo "$x1"
  fi
 done
done

This User Gave Thanks to MadeInGermany For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #479
Difficulty: Medium
A address usually refers to a single byte of memory (called bitwise-addressing).
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

How can I find "-" ni the string variable?

I have 100 strings, which have YYYYDDMMHHMMSS in it and only one is YYYYMMDD-HHMMSS. I want to find that dash and replace it. If I check each string, using sed 's/-//g', it shows me warning that - is not found. So I need if ;then sed 's/-//g', but I cannot find correct regular expression to... (11 Replies)
Discussion started by: digioleg54
11 Replies

3. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

4. Shell Programming and Scripting

String variable as part of expression in find command

Hi, I am new in scripting, and I am currently working on a script that will look for other files in a certain directory and exclude some file type. this works fine:Find_File2Exclude=`find ${paths} -maxdepth 1 -type f \( ! -iname '*.out' ! -iname '*.auc' ! -iname '*.cps' ! -iname '*.log' ! -iname... (4 Replies)
Discussion started by: kedd05
4 Replies

5. Shell Programming and Scripting

Grep command to find string in Variable in file2

Hi , I am executing 2 queries and output is saved in file1.txt and file2.txt example of file1.txt Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,, Testing Excel _.xlsx,/Lab/Development and... (3 Replies)
Discussion started by: Sunil Mathapati
3 Replies

6. Shell Programming and Scripting

Sed find exact string and delete line with variable

All, I am trying to read in a variable and search a file then delete based on that string, but i want to match exact word. This works but it matches all, i don't want to match anthing that contains the string, just the exact string. sed -i "/$feedname/d" file I tried sed... (1 Reply)
Discussion started by: markdjones82
1 Replies

7. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

8. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

9. Shell Programming and Scripting

Find and replace string from file which contains variable and path - SH

e.g. /home/$USER/.config replace it with "" (empty) Is this possible? I think you should play a bit with sharps ## and sed:b: (2 Replies)
Discussion started by: hakermania
2 Replies

10. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

Featured Tech Videos