![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Thanks for the info!
|
|||
| Google The UNIX and Linux Forums |