Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-14-2008, 10:02 PM   #1
LQ Newbie
Registered: Sep 2008
Posts: 3

Rep: Reputation: 0
problems with variables using sed or awk in script

I am trying to compare the differences in two variables to make a third variable. Much like running 'diff file1 file2' i am trying to run
FILE3=`echo $FILE1 | sed -e "s/$FILE2//g"
this is working correctly if $FILE2 is something simple like a line of text.

The problem i am running into is: $FILE2, in my case, has a "." (for example two names: "john.larson kyle.hyde") in it and sed is trying to interpret the "." as a special character. I cant escape it because its inside the variable. Awk is giving the same behavior with something like
FILE3=`echo $FILE1 | awk -v FILE2=$FILE2 '{gsub(FILE2, "")'`
but it complains the the "." in the variable is a syntax error, and again i cant escape it becuase it is in a variable.

I have also tried something like
FILE3=`echo ${FILE1//FILE2/}`
and this doesnt seem to error at all but it is not actually removing the FILE2 information from FILE1.

Because of the functions later in the script, i need to keep the periods as they are. Right now i have this working by sending the command out to two text files and comparing them with diff. If possible i would like to do this in the script using only variables.
Old 09-14-2008, 10:41 PM   #2
Mr. C.
Senior Member
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
You can use perl's \Q \E quoting characters to avoid special character interpretation.

$ FILE1='tom.foolery'; FILE2='tom.fool' ; echo $FILE1 | perl -pe "s/\Q$FILE2\E//"     

$ FILE1='tom foolery'; FILE2='tom.fool' ; echo $FILE1 | perl -pe "s/\Q$FILE2\E//" 
tom foolery
Old 09-14-2008, 11:48 PM   #3
LQ Newbie
Registered: Sep 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Unfortunatley this is a pretty basic load and perl is not part of it. I was looking at some examples of using & within gsub like
gsub(&FILE2, "")
but again it complains about syntax
Old 09-14-2008, 11:51 PM   #4
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 336

Rep: Reputation: 141Reputation: 141
In Bash this removes every match of $FILE2 from $FILE1
You can also escape the '.' for sed.
Here ${FILE2//./\\.} replaces all '.' in $FILE2 with '\.'
FILE3=`echo "$FILE1" | sed -e "s/${FILE2//./\\.}//g"`
Old 09-15-2008, 12:13 AM   #5
LQ Newbie
Registered: Sep 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Excellent, i guess i was just missing some double quotes, this worked great
FILE3=`echo ${FILE1//"FILE2"/}
Thanks again


awk, sed, variable

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with a script that involves awk and sed LinuxLala Programming 2 09-06-2007 10:10 AM
sed and awk in shell script bondoq Linux - Newbie 14 07-27-2007 02:52 AM
ssimple shell script to parse a file ~sed or awk stevie_velvet Programming 7 07-14-2006 04:41 AM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 09:24 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 05:32 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:01 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration