The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
grep unix.com with google



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-07-2007
Registered User
 

Join Date: Nov 2007
Posts: 2
Unhappy Using Awk in shell script to extract an index of a substring from a parent string

Hi All,

I am new to this shell scripting world. Struck up with a problem, can anyone of you please pull me out of this.

Requirement : Need to get the index of a substring from a parent string
Eg : index("Sandy","dy") should return 4 or 3.

My Approach :

I used Awk function index to achieve this

when i excecute this in command line
awk 'BEGIN { print index("Attempting to connect to ipaddr: 127.0.0.1 IllegalUrl_110707092804.txt Succeeded with no errors.","Succeeded with no errors") }'
72

if gives me correct output which is 72

but in shell script it always returns 0.

$a="Attempting to connect to ipaddr: 127.0.0.1 IllegalUrl_110707092804.txt Succeeded with no errors."

$b="Succeeded with no errors"

echo "`awk 'BEGIN { print index($a,$b) }'` "

Can anybody please tell what is the problem or suggest an alternate approach.

Many Thanks,

Sandeep
  #2 (permalink)  
Old 11-07-2007
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,075

Code:
% a="Attempting to connect to ipaddr: 127.0.0.1 IllegalUrl_110707092804.txt Succeeded with no errors."
% b="Succeeded with no errors"
% awk -v a="$a" -v b="$b" 'BEGIN{print index(a,b)}'
72

Use nawk or /usr/xpg4/bin/awk on Solaris.
  #3 (permalink)  
Old 11-07-2007
Registered User
 

Join Date: Nov 2007
Posts: 2
Hey Radoulov,

Thanks a lot

Thanks,
Sandeep
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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Substring in shell script jyotib Shell Programming and Scripting 5 01-16-2008 07:58 PM
help for shell script of finding shortest substring from given string by user pankajd Shell Programming and Scripting 1 11-22-2007 12:27 PM
Substring in Shell Script smartbuddy UNIX for Dummies Questions & Answers 3 08-17-2007 10:26 AM
command/script to extract a substring from a string girisha Shell Programming and Scripting 5 09-21-2006 01:30 PM
Substring in C shell script? dinodash Shell Programming and Scripting 0 03-20-2005 01:59 PM



All times are GMT -4. The time now is 01:22 PM.


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