LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 09-14-2008, 09:02 PM   #1
FeatherKing
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
Code:
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
Code:
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
Code:
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, 09:41 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

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

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

$ FILE1='tom foolery'; FILE2='tom.fool' ; echo $FILE1 | perl -pe "s/\Q$FILE2\E//" 
tom foolery
 
Old 09-14-2008, 10:48 PM   #3
FeatherKing
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
Code:
gsub(&FILE2, "")
but again it complains about syntax
 
Old 09-14-2008, 10:51 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
In Bash this removes every match of $FILE2 from $FILE1
Code:
FILE3=${FILE1//"$FILE2"/}
You can also escape the '.' for sed.
Here ${FILE2//./\\.} replaces all '.' in $FILE2 with '\.'
Code:
FILE3=`echo "$FILE1" | sed -e "s/${FILE2//./\\.}//g"`
 
Old 09-14-2008, 11:13 PM   #5
FeatherKing
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
Code:
FILE3=`echo ${FILE1//"FILE2"/}
Thanks again
 
  


Reply

Tags
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 09:10 AM
sed and awk in shell script bondoq Linux - Newbie 14 07-27-2007 01:52 AM
ssimple shell script to parse a file ~sed or awk stevie_velvet Programming 7 07-14-2006 03:41 AM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM


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

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