The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 05:55 PM
Special Characters in directory name tez UNIX for Dummies Questions & Answers 6 08-14-2006 07:06 PM
handling special characters effigy UNIX for Advanced & Expert Users 1 06-06-2005 10:52 AM
special characters nawnaw UNIX for Dummies Questions & Answers 2 05-18-2004 03:17 PM
Changing Special Characters Using Sed cstovall Shell Programming and Scripting 4 09-15-2003 06:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-03-2002
apalex apalex is offline
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?
  #2 (permalink)  
Old 05-03-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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.
  #3 (permalink)  
Old 05-03-2002
apalex apalex is offline
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!!!
  #4 (permalink)  
Old 05-06-2002
apalex apalex is offline
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.
  #5 (permalink)  
Old 05-06-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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.
  #6 (permalink)  
Old 05-06-2002
apalex apalex is offline
Registered User
  
 

Join Date: May 2001
Location: NY
Posts: 39
Thanks for the info!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0