Hi,
I am writing an awk script and I have got stuck. I am trying to find a word that has 2 dollar signs in it but its failing. I have already tried to escape it with single and double back slash and also I have tried to use \044 in the search but even that doesnt work. can you please help me with the pattern?
input: Hello world $string$
Expected output: string
I have already tried below patterns
echo "Hello world $string$" | awk '/\$.*\$/ { print "match found" }'
echo "Hello world $string$" | awk '/\\$.*\\$/ { print "match found" }'
echo "Hello world $string$" | awk '/\$\$/ { print "match found" }'
echo "Hello world $string$" | awk '/\\$\\$/ { print "match found" }'
echo "Hello world $string$" | awk '/\044.*\044/ { print "match found" }'
Hello,
Could you please try the following code.
echo "Hello world "$"string"$"" | awk -F" " '{print$3}'
Output will be as follows.
$string$
kindly let me know if you have any queries.
Thanks,
R. Singh
Use single quote with echo
instead of double quotes, so that shell doesn't interpret $string
as a variable.
echo 'Hello world $string$' | awk '/\$.*\$/ { print "match found" }'
Klashxx
September 2, 2013, 3:04am
4
Use single quoting to prevent the shell from treating the $ and your test will work:
# echo 'Hello world $string$'|awk '/\$[a-z]*\$/ { print "match found" }'
match found
To print the strings that are enclosed by $ in a file :
awk '{for (i=1;i<=NF;i++) if($i ~ /^\$/ && $i ~ /\$$/) {print substr($i,2,length($i)-2)} }' inputfile
Input:
Hello world $string$
abc
def $kk$ df
Output:
string
kk
Two levels of escape, one for the shell and one for the RegExp. Either '\$'
or '[$]'
sed -n 's/.*[$]\([^$]*\)[$].*/\1/p'
echo needs one level (for the shell), either '$'
or \$
Jotne
September 2, 2013, 3:12am
7
echo 'Hello world $string$' | awk -F\$ '{print $2}'
string
You need to use single quote '
not double quote "
. With double quote it threat $string as a variable and tries to substitute it.
That was really dumb of me.
may be I was a bit tired to notice the obvious. Anyway, that was a quick and great help. Thanks!!