Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a string in my shell script which is in a fixed format : '[STATUS REPORT] PROJECT'. When user executes my shell script he will be asked to provide a value for 'PROJECT'.

I would like to replace the word 'PROJECT' with the user provide value. For Eg if 'ABCD' was user input:

'[STATUS REPORT] ABCD'

I have two issues: 1: How to tackle special characters like '&' in a project name? For eg:

echo "[STATUS REPORT] PROJECT" |  awk '{ gsub(/PROJECT/, "A&A"); print }' 

and I get the following output:

[STATUS REPORT] APROJECTA

2: My actual shell statement looks like this:

echo "[STATUS REPORT] PROJECT" |  awk '{ gsub(/PROJECT/, $ProjectName); print }'

where $ProjectName stores the project name provided by user. But this doesn't seem to work

How can I get this working properly?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

This will solve your 1st issue:

echo "[STATUS REPORT] PROJECT" |  awk '{ gsub(/PROJECT/, "A\\&A"); print }'

(& is a special char in awk substitution: it means "what has been matched". You need to escape if with a \. And as you are in a awk string, your need to put 2 \ to produce one as \ escape the next char as in \n.)

This will solve your 2nd issue:

ProjectName=toto
echo "[STATUS REPORT] PROJECT" |  awk -v "ProjectName=$ProjectName" '{ gsub(/PROJECT/, ProjectName); print }'

(Edit: notice the newly added " around ProjectName=$ProjectName so a space in ProjectName does not break the script.)

But awk is a big tool to perform string substitution, you might want to use sed:

echo "[STATUS REPORT] PROJECT" |  sed -e 's/PROJECT/A\&A/'
ProjectName=toto;
echo "[STATUS REPORT] PROJECT" |  sed -e "s/PROJECT/${ProjectName//\//\\/}/"

(Edit: a / in ProjectName will was breaking the initial solution. I added the escaping of slashes, but some other char(s) still breaks it: &, \1, ... I think plain bash shell bellow is safer.)

or in plain bash shell:

msg="[STATUS REPORT] PROJECT"
echo ${msg//PROJECT/A&A}
ProjectName=toto
echo ${msg//PROJECT/$ProjectName}
share|improve this answer
1  
To be pedantic, this is not true: "& is a special char in regular expressions" -- & is special in the replacement string of the sub and gsub commands. –  glenn jackman Aug 10 '11 at 10:24
    
@glenn: true, I updated the answer. –  jfgagne Aug 10 '11 at 10:59

(Sorry, I don't have commenting privileges here)

From the snippet you've posted it's not very clear why you need to pipe the string to awk.

You can simply use the variable within the echo statement and bash will replace the variable:

echo "[STATUS REPORT] $ProjectName"
share|improve this answer
    
This shell script is part of a GUI App. The "[STATUS REPORT] PROJECT" line will be a user preference. A user can set this line to "PROJECT [STATUS REPORT]" as his preference. I dont want to hard code this. Hence I'm looking to replace this programmatically –  smokinguns Aug 10 '11 at 6:02
    
OK, I See. It looks like jfgagne's awk based solution may work better for you. Since ProjectName is declared as an awk variable, you shouldn't have any problems with special characters. –  Nithin Philips Aug 10 '11 at 7:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.