The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 03:05 PM
how to pass variable to grep? xist Shell Programming and Scripting 2 11-29-2007 07:45 AM
setting variable from grep | cut doublejz Shell Programming and Scripting 1 08-24-2005 03:39 PM
How to grep a variable? whatisthis Shell Programming and Scripting 2 09-14-2004 06:26 PM
Grep from a file variable jagannatha UNIX for Dummies Questions & Answers 5 10-30-2003 05:44 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-10-2006
3Gmobile 3Gmobile is offline
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 !!
  #2 (permalink)  
Old 08-10-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
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.
  #3 (permalink)  
Old 08-10-2006
3Gmobile 3Gmobile is offline
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!
  #4 (permalink)  
Old 08-10-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
  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
  #5 (permalink)  
Old 08-10-2006
3Gmobile 3Gmobile is offline
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 . . .
  #6 (permalink)  
Old 08-11-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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-11-2006 at 12:14 AM..
  #7 (permalink)  
Old 08-10-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

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

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

Last edited by tmarikle; 08-10-2006 at 11:44 PM.. Reason: Duplicate answers...again
Closed Thread

Bookmarks

Tags
grep or

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0