question on sed grep awk from variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting question on sed grep awk from variable
# 1  
Old 08-10-2006
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  
Old 08-10-2006
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  
Old 08-11-2006
# 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  
Old 08-11-2006
  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  
Old 08-11-2006
Why would you want to test $a like that?

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

Last edited by tmarikle; 08-11-2006 at 12:44 AM.. Reason: Duplicate answers...again
# 6  
Old 08-11-2006
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 . . .
# 7  
Old 08-11-2006
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 01:14 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep / awk /sed question

Hi All, I have a file with following sample data. I am also attaching the file. (Est) (Est) Jobs Sch Workstation Job Stream SchedTime State Pr Start Elapse # OK Lim POOL #ACR_BILLING 0005 08/15 ... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

2. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

3. Shell Programming and Scripting

Assign grep match to variable question

Hi, I'm trying to assign a grep result to a variable but instead of having all grep result's assigned to the variable, would it be possible to assign the first match, do something, then move onto the next match and assign it to that variable and so on until all matches have been completed I... (4 Replies)
Discussion started by: Jazmania
4 Replies

4. Shell Programming and Scripting

Question grep and sed

hi everybody i have this script and it work's if i use a single variable in grep, but when i put $searchterm_ the script stops work i have a problem too in sed, because i don't know how i can search and replace more than one item, in this case is >>> $searchterm/$replaceterm and... (4 Replies)
Discussion started by: felito
4 Replies

5. UNIX for Dummies Questions & Answers

sed/grep string replace question

Hi all, I know this question has probably been answered before, but I am struggling with this problem, even after googling a million pages. In a file named rdmt.conf I need a single character replaced, the number in the line below CUR_OC4J_ID=1 It will always appear after... (3 Replies)
Discussion started by: Mike AAA
3 Replies

6. Shell Programming and Scripting

BASH: How do I grep on a variable? (or simmilar question that makes sense)

Hi, I've been running code which very frequently calls books.csv. e.g: grep -i horror books.csv > tempExcept, I'm trying to move away from using temporary files or frequently calling books.csv to improve efficiency. So I tried something like bookfile=$(cat books.csv) grep -i horror... (4 Replies)
Discussion started by: Quan
4 Replies

7. Shell Programming and Scripting

General question about the relationship between KSH and sed/grep/awk etc

Greetings all, Unix rookie here, just diving into ksh scripting for the first time. My question may seem confusing but please bear with me: If I'm understanding everything I'm reading properly, it seems like the ksh language itself doesn't have a lot of string manipulation functions of... (2 Replies)
Discussion started by: DalTXColtsFan
2 Replies

8. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies

9. Shell Programming and Scripting

sed / grep question

Hello, I am new to shell scripting. I have a input file: Few lines of the input file has the following. /a0012/abcd12/abcd12 /a0003/xyzab1/lmno123 /a0006/pqrst1/abcde12 In my output file, I only need to get anything that is between the first and second '/' of each line: exampple:... (6 Replies)
Discussion started by: hemangjani
6 Replies

10. Shell Programming and Scripting

grep & sed question

I'm trying to write a bash script to perform a tedious task, but I have no experience and hardly any knowledge so I've been having a rough time with it. I'm on Mac OS X, and I want a script to do the following: I have a directory that has about 200 sudirectories. In each of these directories,... (1 Reply)
Discussion started by: der Kopf
1 Replies
Login or Register to Ask a Question