The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Handling special characters using awk sam_78_nyc Shell Programming and Scripting 3 11-08-2007 02:55 PM
Special Characters in directory name tez UNIX for Dummies Questions & Answers 6 08-14-2006 04:06 PM
handling special characters effigy UNIX for Advanced & Expert Users 1 06-06-2005 07:52 AM
special characters nawnaw UNIX for Dummies Questions & Answers 2 05-18-2004 12:17 PM
Changing Special Characters Using Sed cstovall Shell Programming and Scripting 4 09-15-2003 03:25 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-03-2002
Registered User
 

Join Date: May 2001
Location: NY
Posts: 39
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 are not familiar with unix)...
help is very much appreciated!

#!/bin/ksh

case $# in
0) echo "\n\t No argument(s) entered! Try again."
echo "\t Usage: whereis arg1 [arg2] [arg3]"
exit 1 ;;
*) grep -n "$*" file | cut -f1 -d: > lineno
cat lineno ;;
esac

if sed/awk can be used, how can i print the line numbers?
Reply With Quote
Forum Sponsor
  #2  
Old 05-03-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,653
The shell does processing to command lines long before it invoke the command. A command like:
something $xyz
is just not going to fly. The shell is going to look for a variable called xyz and substitute that value. Compare the above command to:
echo $PATH

The variable names are gone before the command runs.

Maybe you could read in the string instead of entering it on the command line?
Code:
#! /usr/bin/ksh
IFS=""
print -n "enter search string -"
read string
sed -n '/'"${string}"'/=' < file
exit 0
should do it if you just want only the line number printed.
Reply With Quote
  #3  
Old 05-03-2002
Registered User
 

Join Date: May 2001
Location: NY
Posts: 39
Thanks Perderabo!

i tried your code and it works great. although i had an error
when i entered the string df/DF.

apalex>cat infile
pattern1 $Major
pattern2 -Crit
pattern3 df/DF
pattern4 h'405
pattern5 \ffe
pattern6 ab >
pattern7 abc >>
pattern8 A"a"
pattern9 `0de`

apalex> cat whereis
#! /usr/bin/ksh
IFS=""
print -n "enter search string -"
read string
sed -n '/'"${string}"'/=' < infile
exit 0

apalex> whereis
enter search string -df/DF
sed: command garbled: /df/DF/=
apalex>


I just thought its easier to type the string on the
command line, so i tried to insert your code in
my script but it really gives me a lot of errors...

apalex> cat whereis1
#!/bin/ksh
case $# in
0) echo "\n\t No pattern(s) entered! Try again."
echo "\t Usage: whereis1 pattern1 [pattern2]"
exit 1 ;;
*) sed -n '/'"${*}"'/=' < infile
exit 0 ;;
esac

it doesn't work on patterns:
$Major, ab > and abc >>

I really appreciate this forum, gives me a chance to improve my
skills. Thanks a lot guys!!!
Reply With Quote
  #4  
Old 05-06-2002
Registered User
 

Join Date: May 2001
Location: NY
Posts: 39
Perderabo, can you please explain further the line below?
i can't follow the logic of using ", ' ,= and {} on the sed.

sed -n '/'"${string}"'/=' < infile

Is there any fix in the code for searching for string with "/" on it?
Please see below. Thank you.
Reply With Quote
  #5  
Old 05-06-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,653
Well ${string} is a variable reference like $string but more secure for adjacent characters. I could probable get away with just $string in this case.

I put the variable refernce in double quotes to protect any white space in the value. Everything else in the sed command is in single quotes to prevent anything at all from happening to them.

But the quotes and braces are shell things not sed things. sed will never see them.

As for dealing with the slash, it is possible but would require so much code that I would drop ksh and switch to c. When a script becomes more complex than the c program would be, it's time to switch languages.
Reply With Quote
  #6  
Old 05-06-2002
Registered User
 

Join Date: May 2001
Location: NY
Posts: 39
Thanks for the info!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0