Confusing sed error message


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Confusing sed error message
# 1  
Old 12-16-2018
Confusing sed error message

This situation is extracted from a larger context. My intention for now is to escape the forward slashes in the path of a filename. (Ultimately the LINEs will come from a file.)
Code:
while read LINE ; do
        sed 's/\//\\\//g' <<< "$LINE"    # ok
        escaped=`sed 's/\//\\\//g' <<< "$LINE"`   # error message
        echo $escaped
done <<here
==: dir1/dir2/file1 dir3/dir4/file2
==: dir5/dir6/file3 dir1/file4
==: dir3/file5 dir3/file6
==: dir1/file4 dir5/dir6/file3
==: dir3/dir4/file2 dir1/dir2/file1
==: dir3/file6 dir3/file5
here

While the direct sed output looks the way I want the next line where I try to assign that to a variable gives me an error message:
Code:
==: dir1\/dir2\/file1 dir3\/dir4\/file2
sed: -e expression #1, char 9: unknown option to `s'

What's the problem?

--- Post updated at 11:57 AM ---

Well... when I use $() instead of the `` it works as it should:
Code:
while read LINE ; do
        sed 's/\//\\\//g' <<< "$LINE"
#        escaped=`sed 's/\//\\\//g' <<< "$LINE"`   # doesn't work
        escaped=$(sed 's/\//\\\//g' <<< "$LINE")   # works
        echo $escaped \<-\$escaped
done <<here
==: dir1/dir2/file1 dir3/dir4/file2
==: dir5/dir6/file3 dir1/file4
==: dir3/file5 dir3/file6
==: dir1/file4 dir5/dir6/file3
==: dir3/dir4/file2 dir1/dir2/file1
==: dir3/file6 dir3/file5
here

Output:
Code:
==: dir1\/dir2\/file1 dir3\/dir4\/file2
==: dir1\/dir2\/file1 dir3\/dir4\/file2 <-$escaped
==: dir5\/dir6\/file3 dir1\/file4
==: dir5\/dir6\/file3 dir1\/file4 <-$escaped
etc.

But what's the difference between the two? The Bash Reference doesn't mention any.

Last edited by Ralph; 12-16-2018 at 05:42 AM..
# 2  
Old 12-16-2018
There are spaces in the string
Code:
escaped=`sed 's/\//\\\//g' <<< "$LINE"`   # error message

change so
Code:
escaped="$(sed 's/\//\\\//g' <<<"$LINE")"

separator in s (subtitution) can be any character for example % or |

excuse me may be
Code:
read -r


Last edited by nezabudka; 12-16-2018 at 03:30 PM..
# 3  
Old 12-16-2018
Right. But my main question is now why does this work
$(sed 's/\//\\\//g' <<< "$LINE")
but not this:
`sed 's/\//\\\//g' <<< "$LINE"`
# 4  
Old 12-16-2018
it's works
Code:
escaped=`sed 's|/|\\\/|g' <<< "$LINE"`

and so
Code:
escaped=`sed 's/\//\\\\\//g' <<< "$LINE"`


Last edited by nezabudka; 12-16-2018 at 10:13 AM..
# 5  
Old 12-16-2018
Code:
This
escaped=`sed 's|/|\\\/|g' <<< "$LINE"``
and this
escaped=`sed 's/\//\\\\\//g' <<< "$LINE"`

is not this
Code:
`sed 's/\//\\\//g' <<< "$LINE"`

Thanks for your suggestions, though. I'll look into it. For now I will have to stick to
Code:
$(sed 's/\//\\\//g'  <<< "$LINE")

--- Post updated at 03:15 PM ---

Actually, what I'm trying to do is remove duplicate pairs from a file like this:
Code:
==: dir1/dir2/file1 dir3/dir4/file2
==: dir5/dir6/file3 dir1/file4
==: dir3/file5 dir3/file6
==: dir1/file4 dir5/dir6/file3
==: dir3/dir4/file2 dir1/dir2/file1
==: dir3/file6 dir3/file5

I find out it doesn't really work if I redirect the file into a while-loop that uses read to read a line, like this:
Code:
while read $LINE ; do
   swap column 2 with column 3
   remove swapped line from file (using sed)
done < file

I got the idea because while read works line by line from the beginning of the file the swapped line is always located behind the other one so if I remove it read will never see it. But apparently the entire original file is still available to read no matter what I remove.

Is there is a better approach?
# 6  
Old 12-16-2018
Backticks have been deprecated for a long time. They offer no advantage over $( ... ) and have quoting nesting and escaping issues.

Unless you are writing for a legacy shell like pre-Posix Bourne shell, use $( ... ) instead. I never use them.

Unless you need further line level processing in shell, you could of course use:
Code:
sed 's|/|\\/|g' << "here"
...
here

If you need the line processing in a loop, calling an external program with each iteration is expensive..
If you use bash, ksh93 or zsh as a shell you could use something like this (parameter expansion):
Code:
while read LINE
do
  escaped=${LINE//\//\\/} 
  echo "$escaped"
done << "here"
...
here

-or-

Feed the sed output into a loop (and use read's -r option):
Code:
{
  sed 's|/|\\/|g' << "here"
...
here
} |
while read -r LINE
do
  echo "processing ${LINE}"
done

--
Note: as was suggested a different delimiter removes the need for the escape for the forward slash, which makes the code more readable. In the examples given there was one escape too many:
Code:
sed 's|/|\\/|g'

Also, to prevent headaches, it is recommended to quote variable expansions, so use:
Code:
echo "$escaped"

or better yet:
Code:
printf "%s\n" "$escaped"


Last edited by Scrutinizer; 12-16-2018 at 11:49 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 12-16-2018
Quote:
Originally Posted by Ralph
[..]
--- Post updated at 03:15 PM ---

Actually, what I'm trying to do is remove duplicate pairs from a file like this:
Code:
==: dir1/dir2/file1 dir3/dir4/file2
==: dir5/dir6/file3 dir1/file4
==: dir3/file5 dir3/file6
==: dir1/file4 dir5/dir6/file3
==: dir3/dir4/file2 dir1/dir2/file1
==: dir3/file6 dir3/file5

I find out it doesn't really work if I redirect the file into a while-loop that uses read to read a line, like this:
Code:
while read $LINE ; do
   swap column 2 with column 3
   remove swapped line from file (using sed)
done < file

I got the idea because while read works line by line from the beginning of the file the swapped line is always located behind the other one so if I remove it read will never see it. But apparently the entire original file is still available to read no matter what I remove.

Is there is a better approach?
Assuming the fields in your input file are whitespace separated, you could try this approach:
Code:
awk '!A[$2,$3]++ && !A[$3,$2]++' file

These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confusing of some basic awk

1. increase file space first, double space a file: awk '1;{print ""}' I probably can understand it:print a blank line every time.But when I read triple space a file I am confused: awk '1;{print "\n"}' doesn't it meaning print a blank line every time too? 2. number each line of file, but... (6 Replies)
Discussion started by: hhdzhu
6 Replies

2. Shell Programming and Scripting

Ps command output confusing

Hi, I ran a script named cat item when I searched for this script using command PS I get two process . I don't understand this. Also this script has run for 15 minutes but the time is showing as 0:00:confused::confused: ps -ef | grep cat_item catmgr 4508 4486 0 05:10:29 ? 0:00... (5 Replies)
Discussion started by: TomG
5 Replies

3. Shell Programming and Scripting

sed to extract a multiline subject from a mail message

I'm trying to extract a subject from a mail message but my subject example has 2 lines. How can I manage to extract it and write a string at the end of it? Consider this example: From: test@domain.com Subject: Re: U =?ISO-8859-1?Q?qu=EA=3F!=3F!=3F!!_wtff_=E7=E3o_=ED=F3?= ... (6 Replies)
Discussion started by: twisterbr
6 Replies

4. Shell Programming and Scripting

sed garbled error message in bash shell

Sed garbled error. Cannot determine why the sed command to insert a line at the beginning of a file will not work on declared variables. outfile='DAR.V2.2012115.1.CSV' testfile='totality_request.sql' header_prefix='DATA FILE' no_ext_file=`echo $outfile |sed 's/\(.*\)..../\1/'` ... (6 Replies)
Discussion started by: smenago
6 Replies

5. Shell Programming and Scripting

Confusing find command option

Hi, I am a little bit confusing of using find command. Actually, I am planning to delete the files whatever the files are existing in the day before yesterday. So, I am writing the command like this. find . -name "*.txt" -ctime -2 { here I am confusing, if I will use +2 or +1 also I am... (5 Replies)
Discussion started by: nagraju.allam
5 Replies

6. UNIX and Linux Applications

pikdev requirements confusing

I am looking at installing PiKdev which needs libqt3-mt and kdelibs4-dev. The installed package is qt-r1008952-i486-1 which claims to be a gui toolkit. find / -name "*libqt*" yields nothing with mt just a lot of support, compatible, and access widgets. Normally I would consider this a no go but... (0 Replies)
Discussion started by: slak0
0 Replies

7. Shell Programming and Scripting

Confusing me......!!!!!!

Hiii... There... I am making a Script in which I am taking the value of a variable "var" through key board. But I want, if no values are supplied for "var" for more than 5 seconds then script shuld automatically exit.Script is as follow : #cat abc #!/bin/bash echo "Enter Your Choice : "... (4 Replies)
Discussion started by: prashantshukla
4 Replies

8. Shell Programming and Scripting

Confusing Error

Hi all, Just subscribed to this forum. Not a regular user of Unix.:) I did the following: We have a directory structure /a/b/c5/ Where c5 is the only directory inside b. export ANOOP=/a/b/c*/ echo $ANOOP=/a/b/c5/ I have to create a symbolic link to anoop.txt in the directory... (2 Replies)
Discussion started by: Pankajakshan
2 Replies

9. Solaris

(Need Help) confusing format on solaris 10

Hi All, Very need help about format syntax on solaris 10. I have done install Solaris 10 OS on sun fire v245 but currently i have a problem to use "format" command to display partition info for my hard drive. i cannot enter the format menu, below is captured display : # format Searching... (7 Replies)
Discussion started by: bucci
7 Replies
Login or Register to Ask a Question