sed commands success / fail from commandline vs ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed commands success / fail from commandline vs ksh script
# 1  
Old 07-17-2014
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:
Code:
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:
Code:
 
>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
Code:
 
#!/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
# 2  
Old 07-17-2014
How about this:

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

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

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

# 3  
Old 07-17-2014
Seems like you have the single quote before the p in your script. Might that be the reason?
Try also
Code:
sed -rn '/\\|\//p' file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 07-17-2014
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:

Code:
#!/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

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 07-18-2014
Quote:
Originally Posted by Chubler_XL
How about this:

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

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

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

ChublerXL.. I went with sed coupled with read in raw mode.. seems to work fine.. thanks
Code:
 
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..
Code:
 
sed -rn '/[\/\\]/p' ./myFile.txt
sed: illegal option -- r
sed -n -r '/[\/\\]/p' ./myFile.txt
sed: illegal option -- r

. thanks again..
Mark
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script executed by Cron or commandline

Hello all, I have a question regarding the difference betwen cron and command line. What I would like to do is to print a statement into a logfile if a script has been executed from cron or from command line. It should be as: #!/bin/bash if <Check if this script has been... (3 Replies)
Discussion started by: API
3 Replies

2. UNIX for Dummies Questions & Answers

Check Success Status Of sed command

How do i check success status of a sed command execution i have the below script not sure if it is right approach to check status of execution using a function. Also it looks like in the below sed command even if the search string doesn't exist in the file it is returning status as success as i... (6 Replies)
Discussion started by: Ariean
6 Replies

3. Shell Programming and Scripting

SED sub commands in KSH not working for me

I am using SED to edit a file (called file) the file contains the word "ERROR" and I want to use SED to: 1. Search for text "ERROR" If found, 2. Append new line with text "hoi" I tried: sed 's/ERROR/ a\hoi' file sed 's/ERROR/ a\ hoi' file I get all the time the error sed:... (7 Replies)
Discussion started by: Alex400
7 Replies

4. Shell Programming and Scripting

Have to learn sed and awk commands in kSH?

Links Please??? (2 Replies)
Discussion started by: Diddy
2 Replies

5. UNIX for Advanced & Expert Users

Remote commands fail for tcsh user

I'm more familiar with bash/ksh that csh/tcsh. With that said, I recently ran across a problem with tcsh. Our system admin recently installed purify on our solaris 8 system. In order to use purify you have to execute a Rational script in order to setup the paths and some environment variables... (3 Replies)
Discussion started by: sszd
3 Replies

6. Shell Programming and Scripting

production issue - shell sqlplus processing sometime success sometime fail

Hi Expert, Below is a real production environment issue: we are using shell script to FTP to a remote server and fetch around 150 files every day, for each file we need to keep a entry inside ORACLE DB table, before insert into table, each file has a associated logid, which need to be... (2 Replies)
Discussion started by: summer_cherry
2 Replies

7. Shell Programming and Scripting

Handling values with space while passing commandline argument from wrapper script in KSH

Hi there, I have a wapper script which passes the argument from command prompt to inner script.. It works fine as long as the argument containing single word. But when value contains multiple word with space, not working as expected. I tried my best, couldn't find the reason. Gurus, pls.... (2 Replies)
Discussion started by: kans
2 Replies

8. Shell Programming and Scripting

How to incorporate mutiple commands in Autosys commandline on UNIX

Hi, I am having a problem running multiple commands in a autosys command line. For example I would like to run a perl script first and then add sleep command after that in the same autosys job like below insert_job: xxxxxxxxx command: `perlscript.pl ; sleep 180` Perlscript.pl... (0 Replies)
Discussion started by: waavman
0 Replies

9. Shell Programming and Scripting

Testing success of AWK code in KSH script

I need a bit of help here on something simple. I have a KSH script (must be KSH) that needs to change 2 positional variables in a CSV script. The CSV script looks like this: 00001,010109,01/01/2009 00:01:01 00008,090509,09/05/2009 13:47:26 My AWK script will change $2 and $3 based... (4 Replies)
Discussion started by: kretara
4 Replies

10. Shell Programming and Scripting

Perl Ping Determine Success or Fail

I know how to ping in Perl. That is easy. What I am wondering is if there is a way for Perl to determine whether the ping was successful or not. Or do I need to save the results out and parse the results seperately looking for the #of tries and successful revieves. Thanks. (1 Reply)
Discussion started by: gdboling
1 Replies
Login or Register to Ask a Question