The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep with variable ozvena Shell Programming and Scripting 5 05-14-2008 12:05 PM
how to pass variable to grep? xist Shell Programming and Scripting 2 11-29-2007 04:45 AM
setting variable from grep | cut doublejz Shell Programming and Scripting 1 08-24-2005 12:39 PM
How to grep a variable? whatisthis Shell Programming and Scripting 2 09-14-2004 03:26 PM
Grep from a file variable jagannatha UNIX for Dummies Questions & Answers 5 10-30-2003 02:44 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-09-2006
Registered User
 

Join Date: Aug 2006
Posts: 18
question on sed grep awk from variable

i am still confusing on how to use sed, grep and awk if the input is not a file but a variable.

such as:

a="hello world"
b="how are you"
c="best wish to you"
d="222,333,444"

what if i want to check which variable $a,$b,$c,$d have contain "you"
what if i want to replace the word "you" to "me"
what if i want to grep the 2nd field of the varible

How should i do ?
Does echo $* must be use in all case ?
Can someone give me a good example on this ?

Many thanks !!
Reply With Quote
Forum Sponsor
  #2  
Old 08-10-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
To use sed(or awk or grep) on a variable :
Code:
echo $variable | sed ....
To check wich variable contains 'you'
Code:
a="hello world"
b="how are you"
c="best wish to you"
d="222,333,444"

# check if variable 'a' contains 'you'

if echo "$a" | grep -q you
then
   echo "Variable a contains 'you'"
fi

# Print variables values which contains 'you'

cat <<EOD | awk -v FS='=' '/you/'
$a
$b
$c
$d
EOD

# Print variables names of variables which contains 'you'

cat <<EOD | awk -v FS='=' '/you/ {print $1}'
a=$a
b=$b
c=$c
d=$d
EOD
To replace the word "you" to "me" :
Code:
a="hello world"
b="how are you"
c="best wish to you"
d="222,333,444"

# Replace 'you' by 'me' in variable b

b=$(echo "$b" | sed 's/you/me/')

# Replace 'you' by 'me' in variablesa b c d

b="how are you"
for var in a b c d
do
   eval value="\$$var"
   new_value=$(echo "$value" | sed 's/you/me/' )
   eval $var="\$new_value"
   # Can be done in one statement :
   # eval $var=\$\(echo "\$$var" \| sed 's/you/me/' \)
done
To grep the 2nd field of the variable
Code:
d="222,333,444"

# grep field 2 of variable d (seperator=,)

field2=$(echo "$d" | cut -d, -f2)

Jean-Pierre.
Reply With Quote
  #3  
Old 08-10-2006
Registered User
 

Join Date: Aug 2006
Posts: 18
# check if variable 'a' contains 'you'

if echo "$a" | grep -q you
then
echo "Variable a contains 'you'"
fi

I have the following output ---->
egrep: illegal option -- q
usage: egrep [ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ...


Also, i have one more question:
how to pass the variable out of awk?
example of my script but did not work.

echo $* | awk -F: '{a=$1 b=$2 c=$3}'
#echo $* | awk -F: '{"a=%s b=%s c=%s\n",$1, $2, $3}' <<- not work too.
print $a
print $b
print $c


thanks you!
Reply With Quote
  #4  
Old 08-10-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
  1. grep != egrep
  2. eval `echo $* | awk -F: '{printf("a=%s; b=%s; c=%s\n", $1, $2, $3)}'`
    print $a
    print $b
    print $c
Reply With Quote
  #5  
Old 08-10-2006
Registered User
 

Join Date: Jan 2005
Posts: 682
Why would you want to test $a like that?

if [[ $a = *you* ]]
then
...
fi

Last edited by tmarikle; 08-10-2006 at 08:44 PM. Reason: Duplicate answers...again
Reply With Quote
  #6  
Old 08-10-2006
Registered User
 

Join Date: Aug 2006
Posts: 18
thanks you

Quote:
Originally Posted by vgersh99
  1. grep != egrep
  2. eval `echo $* | awk -F: '{printf("a=%s; b=%s; c=%s\n", $1, $2, $3)}'`
    print $a
    print $b
    print $c
oh.. both grep and egrep not work too

grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .
Reply With Quote
  #7  
Old 08-10-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,016
what OS are you on?
If on Solaris try '/usr/xpg4/bin/grep'
or try tmarikle's suggestion - much better approach

Last edited by vgersh99; 08-10-2006 at 09:14 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0