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
how to get number char from a string netbanker Shell Programming and Scripting 4 07-16-2008 03:16 PM
last char from a string broli Shell Programming and Scripting 6 12-07-2007 08:02 PM
replacing char with string phani_sree High Level Programming 1 11-20-2006 08:57 AM
string of 7 char length always... thanuman UNIX for Dummies Questions & Answers 3 04-12-2005 12:51 PM
Compare Char to String Phobos High Level Programming 3 04-09-2005 11:01 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-07-2008
ALTRUNVRSOFLN ALTRUNVRSOFLN is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 11
Cool Parsing char string

I am stumped! I need to parse an input parameter to a script that has the form '-Ort'. I basically need 'O', 'r' and 't', i.e. the individual characters in the string parsed.

Since there are no delimiters, I don't know how awk could do this. Can someone tell how to do this, this should be a common task in writing scripts?
  #2 (permalink)  
Old 08-07-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool Does the following help?

Code:
> echo '-Ort' | awk '{print substr($0,2,1)}'
O
> echo '-Ort' | awk '{print substr($0,3,1)}'
r
> echo '-Ort' | awk '{print substr($0,4,1)}'
t
  #3 (permalink)  
Old 08-07-2008
aigles's Avatar
aigles aigles is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
Another Ways (get second char) :
Code:
echo '-Ort' | cut -c2  
expr substr '-Ort' 2 1
Jean-Pierre.
  #4 (permalink)  
Old 08-07-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Code:
echo '-Ort' | awk -F\- '{gsub(".","&\n",$2);printf("%s",$2)}'
  #5 (permalink)  
Old 08-07-2008
broli's Avatar
broli broli is offline
Registered User
  
 

Join Date: Dec 2007
Location: Argentina
Posts: 215
if you are parsing the argunments send by you in a script. like
./yscript.sj -orT

the you could use getops
Getopt and getopts
  #6 (permalink)  
Old 08-07-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
Or this one
Code:
echo '-Ort' | awk 'BEGIN{FS=""}{for(i=1;i<=NF;i++)if($i ~ "[a-zA-Z]"){print $i}}'
Closed Thread

Bookmarks

Tags
arguments, awk, parsing

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 07:42 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