Ksh: Replace backslash characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ksh: Replace backslash characters
# 1  
Old 03-03-2010
Ksh: Replace backslash characters

Hi All,

I have a requirement to read a line from a file with some search string, replace any backslash characters in that line and store in a variable.

Shell script: replace.ksh
Code:
#!/bin/bash
file2=input.rtf
line=`grep "Invoice Number" ${file2} | head -1 | sed 's/\\//g'`
echo "start printing"
echo "line_number:${line}"

The Input file "input.rtf" has the following 3 lines:
Code:
whcjkwekcwn
\ab0\ai0\af1\afs18 \ltrch\fcs0 \b0\i0\f40\fs18\insrsid9709693\charrsid550304 Invoice Number:\cell }
asdagv

When I execute the script, I am getting following error:

Code:
$ replace.ksh
sed: command garbled: s/\//g
start printing
line_number:


My expectation is the line should be converted to
Code:
aab0aai0aaf1aafs18 altrchafcs0 ab0ai0af40afs18ainsrsid9709693acharrsid550304 Invoice Number:acell }

which is not happening.

Can someone please help me to solve this?

Thanks in advance.

prashas_d

Last edited by Scott; 03-03-2010 at 08:44 AM.. Reason: Added more code tags for readability
# 2  
Old 03-03-2010
The heading is ksh but you are using bash but try by adding one more slash in the sed...

Code:
#!/bin/bash
file2=input.rtf
line=`grep "Invoice Number" ${file2} | head -1 | sed 's/\\\//g'`
echo "start printing"
echo "line_number:${line}"

# 3  
Old 03-03-2010
Try this,

Code:
#!/bin/bash
file2=input
line=`grep "Invoice Number" ${file2} | head -1 | sed 's/\\\/a/g'`
echo "start printing"
echo "line_number:${line}"

# 4  
Old 03-03-2010
Interestingly, the alternative command substitution method works just fine with two slashes...

Code:
$ line=$(sed -n '/Invoice Number/ {s/\\//g;p;q;}' $file2)

$ echo $line
ab0ai0af1afs18 ltrchfcs0 b0i0f40fs18insrsid9709693charrsid550304 Invoice Number:cell }

# 5  
Old 03-03-2010
@malcomex99,

Hey Thanks a lot. Keeping three backslashes is providing me the required output. :-)

As per my knowledge, to espace backslash, we need to keep one more backslash.

But why it is expecting to keep an additional backslash here again? Can you please explain if you dont mind?

Thanks,
prashas_d
# 6  
Old 03-03-2010
Yes, at command line, a single backslash is enough to escape but in a script it differs.
# 7  
Old 03-03-2010
That's not because it's in a script, that due to the double shell interpretation when using the archaic but still too much used backticks.

As scottn pointed out, using the alternative "$( ... )" syntax doesn't exhibit that issue (and many others).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace special characters with backslash and character

Hi, I have a string wherein i need to replace special characters with backslash and that character. Ex: If my string is a=qwerty123@!, then the new string should be a_new=qwerty123\@\!\, Thanks (3 Replies)
Discussion started by: temp_user
3 Replies

2. UNIX for Dummies Questions & Answers

Replace backslash at the end of the string using sed command

I have text file which is a tab delimited one. Sample data from the file is shown below: unix is\ great\ os linux\ is superb I want to replace that backslash with empty string preserving the tab delimiter. Output should be unix is great os linux is ... (3 Replies)
Discussion started by: p.akhilreddy4u
3 Replies

3. Shell Programming and Scripting

How to replace spaces excluding those within double quotes and after backslash?

In bash or perl, I would like to know how to substitute a null character (0x00) for every white space without changing the white spaces inside the block of double quotes and the white space immediately following a backslash. Suppose that sample.txt consists of the following line. "b 1" c\ 2 ... (2 Replies)
Discussion started by: LessNux
2 Replies

4. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

5. Shell Programming and Scripting

Replace apostrophe with backslash apostrophe

I'm coding using BASH and have a requirement to replace apostrophes with backslash apostrophes. For example below: I am here 'in my room' ok Would be changed to: I am here /'in my room/' ok The original text is coming from a field in a MySql database and is being used by another process that... (5 Replies)
Discussion started by: dbjock
5 Replies

6. Shell Programming and Scripting

How to replace characters with random characters

I've got a file (numbers.txt) filled with numbers and I want to replace each one of those numbers with a new random number between 0 and 9. This is my script so far: #!/bin/bash rand=$(($RANDOM % 9)) sed -i s//$rand/g numbers.txtThe problem that I have is that it replaces each number with just... (2 Replies)
Discussion started by: hellocatfood
2 Replies

7. Shell Programming and Scripting

SED script to backslash special characters

I have a shell script that I have written to be a kind of to-do/notepad that's quickly executable from the command line. However, special characters tend to break it pretty well. Ie: "notes -a This is an entry." works fine. "notes -a This is (my) entry." will toss back a bash syntax error on... (5 Replies)
Discussion started by: skylersee
5 Replies

8. Shell Programming and Scripting

double backslash in ksh

Hi I need the "\\hello" (without double quotes) to be written to a file. echo "\\\\hello" >file is working under bash shell but not working under ksh shell (gives only one / in the output) Please advise. TIA Prvn (4 Replies)
Discussion started by: prvnrk
4 Replies

9. Shell Programming and Scripting

Sed and awk backslash characters

Hi, I have a variable read from user input: PROFILESROOTDIR="\\194.185.82.188\CMSRepository\EncodingProfiles" awk -F"=" -v gr=$PROFILESROOTDIR '/ProfilesRootDirectoryFromXOEMachine/{$2=gr;}1' OFS="=" $CFGFILE > "${CFGFILE}_new" For this awk to work properly I need to replace in the... (7 Replies)
Discussion started by: potro
7 Replies

10. Shell Programming and Scripting

get last characters in ksh

Hi, Here the way the txt file looks like.. 100|abcd|6899|xyz 112|dlkja|79311| 432|adjkl|1348|iaw I need the last characters after the last '|' .. thanks (5 Replies)
Discussion started by: meghana
5 Replies
Login or Register to Ask a Question