Sed and awk backslash characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed and awk backslash characters
# 1  
Old 05-19-2008
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 variable each \ character with \\\ using sed but I just can't manage to do that.

Thanks,
Bianca
# 2  
Old 05-19-2008
Code:
echo '\\194.185.82.188\CMSRepository\EncodingProfiles' | sed 's%\\%\\\\\\%g'

This User Gave Thanks to agn For This Post:
# 3  
Old 05-19-2008
Quote:
Originally Posted by agn
Code:
echo '\\194.185.82.188\CMSRepository\EncodingProfiles' | sed 's%\\%\\\\\\%g'

If I use this in the script
PROFILESROOTDIR1=`echo $PROFILESROOTDIR |sed 's%\\%\\\\\\%g'`

I get
sed: -e expression #1, char 9: unterminated `s' command


Bianca
# 4  
Old 05-19-2008
I think it should be / instead of % in the code..

Quote:
Originally Posted by potro
If I use this in the script
PROFILESROOTDIR1=`echo $PROFILESROOTDIR |sed 's%\\%\\\\\\%g'`

I get
sed: -e expression #1, char 9: unterminated `s' command


Bianca
# 5  
Old 05-19-2008
Try this:

Code:
PROFILESROOTDIR=$(echo $PROFILESROOTDIR | sed 's%\\%\\\\\\%g')

# 6  
Old 05-19-2008
Quote:
Originally Posted by agn
Try this:

Code:
PROFILESROOTDIR=$(echo $PROFILESROOTDIR | sed 's%\\%\\\\\\%g')

There is a problem with function used to read user's input
readDefault()
{
ARGS=""
N=1
LOCALBUF=""
until [ $N -eq $# ]
do
eval ARG=\${$N}
ARGS=" $ARGS $ARG"
N=`expr $N + 1`
done
read $ARGS LOCALBUF
if [ -n "$LOCALBUF" ]
then
VARNAME=${!#}
export $VARNAME=$LOCALBUF
else
echo "Using current value."
fi
}

echo "Please type directory where the profiles are placed: (current value: $PROFILESROOTDIR)"
readDefault -e PROFILESROOTDIR

In this function the \ character is ignored from variable given by user and removed from it.

DO you have any ideea how to ignore the \ characters in this function?

Thanks,
Bianca
# 7  
Old 05-19-2008
The read command parses backslashes. If you have a reasonably modern shell, it might have read -r for "raw" input without any special treatment of backslashes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk grep, that will only get the line with more characters.

Is there a command for sed and awk that will only sort the line with more characters? #cat file 123 12345 12 asdgjljhhho bac ss Output: asdgjljhhho #cat file2 11.2 12345.00 21.222 12345678.10 (2 Replies)
Discussion started by: invinzin21
2 Replies

2. 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

3. Shell Programming and Scripting

Cut on last backslash on hyperlink string-sed/awk??

hyper link- abc:8081/xyz/2.5.6/rtyp-2.5.6.jar Needs to get "rtyp-2.5.6.jar" i.e character after last backslash "/" how to do this using sed/awk?? help is highly appreciated. (7 Replies)
Discussion started by: kkscm
7 Replies

4. Shell Programming and Scripting

Deleting characters with sed,perl,awk

Input: :: gstreamer :: xine-lib :: xine-lib-extras Output should be: gstreamer xine-lib xine-lib-extras How can it be done with sed or perl? (12 Replies)
Discussion started by: cola
12 Replies

5. 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

6. Shell Programming and Scripting

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 #!/bin/bash file2=input.rtf line=`grep "Invoice Number" ${file2} | head -1 | sed 's/\\//g'` echo "start... (6 Replies)
Discussion started by: prashas_d
6 Replies

7. Shell Programming and Scripting

awk and sed, how to exclude certain characters

Hello everyone: I have ran into this a few times now where my skills are just not up to snuff when it comes to Unix. So, I came here to find some beard stroking Unix wizard to help me. Basically, I am using OS X 10.5 in large scale at work and sometimes I have to run some custom reports. ... (5 Replies)
Discussion started by: tlarkin
5 Replies

8. Shell Programming and Scripting

Awk , Sed Print last 4 numeric characters

Hello All, I have been searching and trying this for a bit now. Can use some assistance. Large 5000 line flat file. bash, rhel5 Input File Sinppet: Fri Oct 30 09:24:02 EDT 2009 -- 1030 Fri Oct 30 09:26:01 EDT 2009 -- 73 Fri Oct 30 09:28:01 EDT 2009 -- 1220 Fri Oct 30 09:30:01 EDT... (9 Replies)
Discussion started by: abacus
9 Replies

9. Shell Programming and Scripting

Extract some characters with SED or AWK

Hi, I have the following example string: today_is_a_good_day.txt The character "_" inside the string can sometimes be more or less. The solution for every string equal the count of "_" should be alway the rest after the last underline character. Result: day.txt I want to use awk... (5 Replies)
Discussion started by: climber
5 Replies

10. Shell Programming and Scripting

awk/sed with special characters

i have this script that searches for a pattern. However it fails if the pattern includes some special characters. So far, it fails with the following strings: 1. -Cr 2. $Mj 3. H'412 would a sed or awk be more effective? i don't want the users to put the (\) during the search (they... (5 Replies)
Discussion started by: apalex
5 Replies
Login or Register to Ask a Question