Reading a file containing escaped sequences

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 3
Reputation: lml108 is an unknown quantity at this point 
Solved Threads: 0
lml108 lml108 is offline Offline
Newbie Poster

Reading a file containing escaped sequences

 
0
  #1
Mar 23rd, 2009
Hi,
I've a file having some regular expressions.
e.g. cat regex.txt
[\t\n]
abc.*pqr

If I write a small bash script as below,

while read line
do
echo -E "$line"
done < regex.txt

even with -E switch passed to echo the output is
[tn]
abc.*pqr

So the backslashes have vanished in the first regular expression.
Is there a way to retain all backslashed escape sequences
while reading a file using the while loop ?

Thanks,
Mahendra
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 54
Reputation: radoulov is an unknown quantity at this point 
Solved Threads: 5
radoulov's Avatar
radoulov radoulov is offline Offline
Junior Poster in Training

Re: Reading a file containing escaped sequences

 
0
  #2
Mar 23rd, 2009
Shell Scripting Syntax (Toggle Plain Text)
  1. while IFS= read -r;do
  2. printf '%s\n' "$REPLY"
  3. done<regex.txt
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: lml108 is an unknown quantity at this point 
Solved Threads: 0
lml108 lml108 is offline Offline
Newbie Poster

Re: Reading a file containing escaped sequences

 
0
  #3
Mar 23rd, 2009
Originally Posted by radoulov View Post
Shell Scripting Syntax (Toggle Plain Text)
  1. while IFS= read -r;do
  2. printf '%s\n' "$REPLY"
  3. done<regex.txt
Thanks Radoulov. It's now reading \ literally without interpreting it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: Reading a file containing escaped sequences

 
0
  #4
Mar 23rd, 2009
Why's it work?

Matter inside double quotes (") is interpreted/evaluated by the shell. Matter inside single quotes (') is not.

It's a handy tool, but can cause some stubbed toes along the way.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 528 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC