how to get first two characters from a word | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

how to get first two characters from a word

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-06-2007
sachin.gangadha sachin.gangadha is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 23 April 2008, 9:40 PM EDT
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
how to get first two characters from a word

Hi guyz,

suppose there is a variable a=sachin.
I want to have letter 'c' from variable a.

I am trying this awk 'substr(sachin,2,1)'
but its not working.

I cant user cut as it requires a file whereas i have a variable.
Sponsored Links
    #2  
Old 12-06-2007
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 30 July 2014, 5:10 AM EDT
Location: /dev/ph
Posts: 4,773
Thanks: 59
Thanked 423 Times in 392 Posts
Here is one way of doing it


Code:
echo "sachin" | awk '{ printf "%s\n", substr($1,1,2) }'

Sponsored Links
    #3  
Old 12-06-2007
broli's Avatar
broli broli is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 23 July 2014, 5:32 PM EDT
Location: Argentina
Posts: 219
Thanks: 0
Thanked 0 Times in 0 Posts
you can use cut
echo $somevar | cut -c 2
i dont know if its the best option, but is the most straight forward
    #4  
Old 12-06-2007
sachin.gangadha sachin.gangadha is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 23 April 2008, 9:40 PM EDT
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
thanks a lot for your replies.
Sponsored Links
    #5  
Old 12-06-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 1 August 2014, 12:18 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,666
Thanks: 182
Thanked 617 Times in 575 Posts
bash and ksh93:


Code:
echo ${a:2:1}

zsh:


Code:
print $a[3]

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find word in file then get following characters TasosARISFC Shell Programming and Scripting 6 08-19-2011 12:19 PM
find a word in a file, plus the next 6 characters? mikayla73 UNIX for Dummies Questions & Answers 6 01-03-2011 12:07 PM
Adding Characters to a Word List evillion Shell Programming and Scripting 7 07-07-2010 01:55 PM
deleting last characters of a word rajaryan4545 Shell Programming and Scripting 3 07-16-2008 01:38 AM
Print the characters in a word chella Shell Programming and Scripting 5 10-17-2007 09:06 AM



All times are GMT -4. The time now is 01:46 AM.