Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 12-06-2007
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 27 July 2015, 7:02 PM EDT
Location: /dev/ph
Posts: 4,941
Thanks: 70
Thanked 461 Times in 427 Posts
Here is one way of doing it


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

Sponsored Links
    #3  
Old Unix and Linux 12-06-2007
broli's Unix or Linux Image
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 Unix and Linux 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 Unix and Linux 12-06-2007
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 April 2015, 3:05 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 626 Times in 584 Posts
bash and ksh93:


Code:
echo ${a:2:1}

zsh:


Code:
print $a[3]

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 04:22 AM.