![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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? |
|
||||
|
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!!! |
|
||||
|
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. |
|
|||||
|
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. |
|
||||
|
Thanks for the info!
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|