2 dollars wrapped to a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 2 dollars wrapped to a string
# 1  
Old 09-02-2013
2 dollars wrapped to a string

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?

Code:
input: Hello world $string$
Expected output: string

I have already tried below patterns

Code:
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" }'


Last edited by Scott; 09-02-2013 at 04:22 AM.. Reason: Quote tags -> Code tags
# 2  
Old 09-02-2013
Hello,

Could you please try the following code.

Code:
echo "Hello world "$"string"$"" | awk -F" " '{print$3}'

Output will be as follows.
Code:
$string$

kindly let me know if you have any queries.


Thanks,
R. Singh
# 3  
Old 09-02-2013
Quote:
Originally Posted by hitmansilentass
I have already tried below patterns
Code:
echo "Hello world $string$" | awk '/\$.*\$/ { print "match found" }'
...
...

Use single quote with echo instead of double quotes, so that shell doesn't interpret $string as a variable.

Code:
echo 'Hello world $string$' | awk '/\$.*\$/ { print "match found" }'

# 4  
Old 09-02-2013
Use single quoting to prevent the shell from treating the $ and your test will work:
Code:
# echo 'Hello world $string$'|awk '/\$[a-z]*\$/ { print "match found" }'                         
match found

# 5  
Old 09-02-2013
To print the strings that are enclosed by $ in a file :

Code:
awk '{for (i=1;i<=NF;i++)  if($i ~ /^\$/ && $i ~ /\$$/)  {print substr($i,2,length($i)-2)} }' inputfile


Input:
Code:
Hello world $string$
abc
def $kk$ df

Output:
Code:
string
kk

# 6  
Old 09-02-2013
Two levels of escape, one for the shell and one for the RegExp. Either '\$' or '[$]'
Code:
sed -n 's/.*[$]\([^$]*\)[$].*/\1/p'

echo needs one level (for the shell), either '$' or \$

Last edited by MadeInGermany; 09-02-2013 at 04:26 AM..
# 7  
Old 09-02-2013
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. What is on Your Mind?

Thank You Patreon Sponsors - Your Patreon Dollars at Work

Thank You Patreon Sponsors! We are using your generous sponsorship dollars for software and media licensing. Your patronage is greatly appreciated! Currently, we are working on new tech videos on the YouTube YT channel, which is the new official media channel for unix.com. ... (0 Replies)
Discussion started by: Neo
0 Replies

2. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 Replies

3. Shell Programming and Scripting

change number into dollars and cents

in ksh. Here is what I have working thanks to all of you. Now I need to take it one step further. The $result value needs to be in the $0000.00 format instead of what it is now, 000000. Yes, it is always 6 characters, I thought this would help but so far it has baffled me. My thought... (3 Replies)
Discussion started by: CougarMutt
3 Replies

4. Shell Programming and Scripting

Pass all received args to a (wrapped) child script

I'm writing a wrapper script (in bash) that wraps another (bash) script. When calling the wrapped script, I need to pass all the received arguments/options to it. Is there a built in variable that holds all the options? I wrote a little while loop (see below) which works. But I wanted to know if... (1 Reply)
Discussion started by: Dilbert
1 Replies

5. Shell Programming and Scripting

Can't get my head wrapped around CGI/Perl

Hello, I am about 3 weeks new to CGI/ Perl scripting and so far some concepts I can wrap my head around perfectly but others not so much. I was wondering if I could get some help in making a script that will pull from a text file and put back into a form. I can make a script that takes input... (15 Replies)
Discussion started by: sennex
15 Replies

6. UNIX for Advanced & Expert Users

convert one colume file to a one line, wrapped file.

I need to convert a file i.e cat list 1000: 1001: 1002: to cat wrappedfile 1000:1001:1002: currently I am using a while loop, paste and mv command to achieve desired outcome. touch wrappedfile cat list | while read line ;do echo $line > /tmp/$line;paste /tmp/$line wrappedfile >... (7 Replies)
Discussion started by: jouuu
7 Replies

7. UNIX for Dummies Questions & Answers

How to eliminate wrapped lines

I have a file abc: line 1 line 2 line 3 line 4 And I am successfully e-mailing the file, with this: mail -s "contents of abc" jdoe@email.com <<EOF cat abc EOF But the e-mail shows up looking like this: subject: contents of abc line 1 line 2 line 3 line 4 The carriage returns... (5 Replies)
Discussion started by: tumblez
5 Replies

8. Shell Programming and Scripting

Merge wrapped lines

Hi, Can someone tell me how i can merge the lines that are wrapped. My file has content somethig like this Line1 -> xxxx bbbb ccc dddd bababab11 Line2 -> 2222 nnn cccc Line 3-> yyyyy zzzz uuzuz ioouo oououou11 Line 4 -> 3333 pppp dddd Line 5-> zzzz kjkj uuzuz ioouo oououou11 Line 6->... (1 Reply)
Discussion started by: braindrain
1 Replies
Login or Register to Ask a Question