griptypethyne Posted March 14, 2005 Report Share Posted March 14, 2005 I am writing a Bash script to convert a text file from one format to another. The input file makes extensive use of the "\" character. I want to retain these characters unchanged. As far as I can tell the "\" is acted upon as soon as a line is read from a file. This means that I do not get the opportunity to escape it or temporarily replace it with another character. One solution is to use a text editor to replace the "\" with a temporary character, run the script and then use the editor to replace the "\". Howver this is a messy way of solving the problem. Is there some way I can tell Bash to ignore "\" as an escape character? Maybe I can run sed at the beginning of and end of the script? Quote Link to comment Share on other sites More sharing options...
sellis Posted March 14, 2005 Report Share Posted March 14, 2005 I'm not sure what you're doing, but could you use sed to replace '\' with '\\' in the input file? This should read in as a '\' and you wouldn't then need a second replace at the end. This, of course, in case you can't actually suppress '\' expansion on reading. Also, have you considered using one of the specialised file manipulation languages (awk, perl) instead of a bash script? Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted March 15, 2005 Report Share Posted March 15, 2005 (edited) I'm not 100% sure of what you are doing or how you are managing it, but if you cat the file in, the \ remain intact: backslash.txt: \test \\test \\40 \\\3 \13 backslash.sh: #!/bin/bash test="$(cat <backslash.txt)" echo "$test" output: \test \\test \\40 \\\3 \13 I am but a lowly bash apprentice...aru or theYinYeti will come along and fix everything. Edited March 15, 2005 by Steve Scrimpshire Quote Link to comment Share on other sites More sharing options...
griptypethyne Posted March 15, 2005 Author Report Share Posted March 15, 2005 Thanks for the quick replies. I am converting Proxoimitron .cfg files to Proximodo filters.txt format. Until I started this project the longest script I had written was three or four lines so I am ging through a steep learning curve. I have not yet considred using awk. I am aware of its existance but first I want to learn to write bash scripts and use regular expressions (-; I have carried out two tests: #exec < $backslashtest #read Line echo line This removed one \. I then tried the CAT example and it did not remove any \ Therefore I have two options - either use cat instead of read or use sed to insert an extra \. Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted March 15, 2005 Report Share Posted March 15, 2005 To be clear, the 'cat' way could be done line by line, as well: #!/bin/bash test="$(cat <backslash.txt)" for line in $test do <<do something with $line here making sure it has "doublequotes" around it>> done Quote Link to comment Share on other sites More sharing options...
griptypethyne Posted March 15, 2005 Author Report Share Posted March 15, 2005 To be clear, the 'cat' way could be done line by line, as well: I will try this as well. I am still coming to grips with the Bash syntax. cat < backslash.txt is a command. Therefore could the test= line also be written using backquotes: test=`cat < backslash.txt` or does that have a different meaning? Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted March 15, 2005 Report Share Posted March 15, 2005 They are slightly different: http://www.tldp.org/LDP/abs/html/commandsub.html aru or theYinYeti can translate it better than I. Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted March 15, 2005 Report Share Posted March 15, 2005 Oh, and something else I just noticed: It is more correct to say either test=$(cat backslash.txt) or test=$(<backslash.txt) rather than both together: test=$(cat <backslash.txt) Quote Link to comment Share on other sites More sharing options...
sellis Posted March 15, 2005 Report Share Posted March 15, 2005 http://www.tldp.org/LDP/abs/html/commandsub.html Hell and bat's do's... that's amazing. I keep having to remind myself that bash scripts are NOT just windows batch files on steroids, but proper programs in their own right. Looks like the "test=$(<backslash.txt)" syntax will answer the original question, then. Quote Link to comment Share on other sites More sharing options...
theYinYeti Posted March 15, 2005 Report Share Posted March 15, 2005 Hello, I'd be glad to help, but I don't get what it is you want to do. As was already said, awk and sed are good tools for manipulating text files, and bash can do usefull stuff too. But in any case, '\' characters shouldn't give you trouble. In what circumstances exactly do you have the problem of '\' disapearing? Yves. Quote Link to comment Share on other sites More sharing options...
griptypethyne Posted March 15, 2005 Author Report Share Posted March 15, 2005 Problem solved! I did what I should have done at the start. I applied the RTFM principle, went to the GNU site and looked up the official Bash documentation. read -r Line treats \ as a normal rather than an escape character. It is interesting what you discover when you read the complete documentation B) . For example -d delim specifies that delim be used to terrminate the input line rather than newline. I am sure that that could be useful somewhere ;) Quote Link to comment Share on other sites More sharing options...
theYinYeti Posted March 16, 2005 Report Share Posted March 16, 2005 Yes, yes, yes, and so on. :) I actually like reading manuals. There's so much to learn inside. bash, awk, and sed are great tools, really. And regular expressions are something you can't live without once you're comfortable with them. Yves. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.