Expansion of variable inside Single Quotes Fails!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expansion of variable inside Single Quotes Fails!!
# 1  
Old 10-04-2016
Expansion of variable inside Single Quotes Fails!!

I am unable to expand the value of entry variable inside the nawk command.

I tried three different nawk command as below but none of them substitute the value of entry variable.

Code:
 
 ls *.txt  | while IFS='' read -r entry; do
#nawk '/<name>/{A=1;++i} A{print >> ("cmd"i"_"$entry)} /<\/name>/{A=0} ' $entry
#nawk '/<name>/{A=1;++i} A{print >> ("cmd"i"_"'$entry')} /<\/name>/{A=0} ' $entry
#nawk '/<name>/{A=1;++i} A{print >> ("cmd"i"_"'"$entry"')} /<\/name>/{A=0} ' $entry
done


Please suggest.
# 2  
Old 10-04-2016
You would need to use double-quotes rather than single quotes so that the shell gets in and expands them for you. This may, however, cause issues with the existing double-quotes.

You can try changing them to single quotes or escaping them.


Does that help?


Robin
# 3  
Old 10-04-2016
I am not sure how can I use double quotes instead of single.

But this is what I tried and it still fails.

Code:
nawk "/<name>/{A=1;++i} A{print >> ("cmd"i"$entry")} /<\/name>/{A=0} " $entry

# 4  
Old 10-04-2016
It would help to show the error returned, however what you are doing with the command like this is to call nawk with confused arguments because you have double quotes throughout.

Does this perform any better?:-
Code:
nawk "/<name>/{A=1;++i} A{print >> ('cmd'i'$entry')} /<\/name>/{A=0} " $entry

It might complain still, but please show us the output, else we're just guessing blind.



Kind regards,
Robin
# 5  
Old 10-04-2016
I tried your last suggestion but it fails with the below error.

Code:
bash: syntax error near unexpected token `('
bash-3.2$ uname -a
SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v

# 6  
Old 10-04-2016
Don't mess around with single/double quotes and shell variable expansion; use the designated method:
Code:
ls *.txt  | while IFS='' read -r entry; do
awk -vENTRY="$entry" '/<name>/ {A=1; ++i} A {print >> ("cmd" i "_" ENTRY)} /<\/name>/ {A=0} ' $entry
done

These 2 Users Gave Thanks to RudiC For This Post:
# 7  
Old 10-04-2016
with this correction in RudiC's suggestion ...it works !!

Code:
awk -v ENTRY="$entry" '/<name>/ {A=1; ++i} A {print >> ("cmd" i "_" ENTRY)} /<\/name>/ {A=0} ' $entry

Still i would like to try rbattle1's suggestions if that gets fixed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed variable expansion fails for substitution in range

I'm trying to change "F" to "G" in lines after the first one: 'FUE.SER' 5 1 1 F0501 F0401 F0502 2 1 F0301 E0501 F0201 E0502 F0302 3 1 F0503 E0503 E0301 E0201 E0302 E0504 F0504 4 1 F0402 F0202 E0202 F0101 E0203 F0203 F0403 5 1 F0505 E0505 E0303 E0204 E0304 E0506... (10 Replies)
Discussion started by: larrl
10 Replies

2. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

3. Shell Programming and Scripting

Unable to echo single quotes inside awk

# echo 'export HISTFILE=/var/log/history/history_$(uname -n)_$(date +%Y:%b:%d:%H:%M)_$(who am i | awk '{print \$1}')' >> new_file # # cat new_file export HISTFILE=/var/log/history/history_$(uname -n)_$(date +%Y:%b:%d:%H:%M)_$(who am i | awk {print $1}) # Now how to echo the quotes around the... (2 Replies)
Discussion started by: proactiveaditya
2 Replies

4. Shell Programming and Scripting

Interpolate a variable with single quotes

I need to interpolate a shell variable in a code, i cannot share the exact code so this is an example i made up to describe the situation What I am trying to do here is try to wrap up the value of a variable in single quotes. This value needs to be passed to another program which would only... (4 Replies)
Discussion started by: sam05121988
4 Replies

5. UNIX for Dummies Questions & Answers

awk for inserting a variable containing single and double quotes

Hi i have to insert the below line into a specific line number of another file export MBR_CNT_PRCP_TYPE_CODES_DEL="'01','02','04','05','49','55','UNK'" I have passed the above line to a variable say ins_line. I have used below command to perform the insert awk 'NR==3{print "'"${ins_line}"'"}1'... (1 Reply)
Discussion started by: sathishteradata
1 Replies

6. Shell Programming and Scripting

Having a terrible problem with quotes/single quotes!

Hello. I'm trying to write a bash script that uses GNU screen and have hit a brick wall that has cost me many hours... (I'm sure it has something to do with quoting/globbing, which is why I post it here) I can make a script that does the following just fine: test.sh: #!/bin/bash # make... (2 Replies)
Discussion started by: jondecker76
2 Replies

7. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

8. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

9. Shell Programming and Scripting

Single quotes and double quotes

Hi guys, I have a sed line in double quotes which works fine, but I want it to be in single quotes here is the sed line sed "/abc_def/s/\'.*\'/\'\${abc_def}\'/" can some one give the equivalent to the above script in single quotes Thanks a ton (5 Replies)
Discussion started by: sol_nov
5 Replies

10. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies
Login or Register to Ask a Question