sed commands success / fail from commandline vs ksh script

solaris 5.10 Generic_138888-03 sun4v sparc SUNW,Sun-Fire-T200

I need a sed command that tests true when presented with lines that contain either forward and backslash.

input file:

c:/myFile.txt
c:\yourFile.txt

It doesn't appear that sed (in my environment anyway) supports alternation.. (perhaps BRE vs extended?), so I tried multiple commands separated by a semi colon.

from the commandline:

 
>sed -n '/^.*\//p; /^.*\\/p' ./myFile.txt
c:/myFile.txt
c:\yourFile.txt

all is well.. (or so it appears.. first shell script in 20 years and as spend most of my time in perl, I've never used sed.. )

cool.. I added it to the shell script..

shell script

 
#!/bin/ksh
#set -xv
file=/myFile.txt
while read line
do
        if [ `echo $line | sed -n '/^.*\//p; /^.*\\/'p` ] ; then
                echo "path/filename: $line\n"
        fi
done < $file
 
sed: command garbled: /^.*\//p; /^.*\/p
sed: command garbled: /^.*\//p; /^.*\/p
... 

I notice that the last slash from the search in the second sed command doesn't show up in the garbled message.. (just before the last print ('p'))
Is that a clue?
Is the escape incorrect?
Multiple command syntax wrong?
I've tried putting single quote around each command..
.. removing the silent flag (-n)..

I'll take any suggestions even related to my attempt at ksh.. as I said.. been a long time. thanks in advance..
Mark

How about this:

sed -n '/[\/\\]/p' ./myFile.txt

Or split you two rules with -e . Also, note that ^.* is not required

sed -n -e '/\//p' -e '/\\/p' ./myFile.txt

Seems like you have the single quote before the p in your script. Might that be the reason?
Try also

sed -rn '/\\|\//p' file
1 Like

As far as your ksh script goes:

read needs -r flag to be able to read backslash character properly
no need to use sed ksh has it's own glob matching either with case or if statements eg:

#!/bin/ksh
#set -xv
file=./myFile.txt
while read -r line
do
   if [[ "$line" = */* || "$line" = *\\* ]] ; then
           echo "IF match: path/filename: $line"
   fi
   case "$line"
   in
     */* | *\\*) echo "CASE match: path/filename: $line" ;;
   esac
done < $file
1 Like

ChublerXL.. I went with sed coupled with read in raw mode.. seems to work fine.. thanks

 
while read -r line
do
        if [ `echo $line | sed -n '/[\/\\]/p'` ] ; then
                echo "path/filename: $line\n"
        fi
done < $file
 

Rudi C much appreciated, but it appears that our version sed has not been cultivated to the point it handles extended regex..

 
sed -rn '/[\/\\]/p' ./myFile.txt
sed: illegal option -- r
sed -n -r '/[\/\\]/p' ./myFile.txt
sed: illegal option -- r

. thanks again..
Mark