![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 !! |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
To use sed(or awk or grep) on a variable :
Code:
echo $variable | sed .... 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
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 Code:
d="222,333,444" # grep field 2 of variable d (seperator=,) field2=$(echo "$d" | cut -d, -f2) Jean-Pierre. |
|
#3
|
|||
|
|||
|
# 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
|
||||
|
||||
|
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
thanks you
Quote:
grep: illegal option -- q Usage: grep -hblcnsviw pattern file . . . |
|
#7
|
||||
|
||||
|
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. |
||||
| Google The UNIX and Linux Forums |