GeekInterview.com
Series: Subject: Topic:
Question: 47 of 55

If you have a string "one two three", Which shell command would you use to extract the strings?

Asked by: Interview Candidate | Asked on: Jul 27th, 2005
Showing Answers 1 - 13 of 13 Answers
Rishi Pahuja

Answered On : Jun 6th, 2005

echo $string | cut -d" " -f1 
echo $string | cut -d" " -f2 
echo $string | cut -d" " -f3 
 
 

  
Login to rate this answer.
abhay

Answered On : Jul 5th, 2005

echo "one two three" | cut -d" " -f 1,2,3 
or 
echo "one two three" | awk '{print $1 $2 $3}' 

Yes  3 Users have rated as useful.
  
Login to rate this answer.
akebono

Answered On : Jul 27th, 2005

sed and awk 

  
Login to rate this answer.
rno

Answered On : Oct 5th, 2006

cut , awk, sed and others are not "SHELL" commands. One way could be with bash: string="one two three"tab=($string)$ echo ${tab[0]}one$ echo ${tab[1]}two$ echo ${tab[2]}threehttp://www.big-up.org

  
Login to rate this answer.
atrain

Answered On : Mar 20th, 2007

echo "one two three" | while read ONE TWO THREE
print $ONE
one
print $TWO
two
print $THREE
three

  
Login to rate this answer.
pranab kumar ghosh

Answered On : Apr 4th, 2007

set -- one two three
echo $1
echo $2
echo $3

  
Login to rate this answer.
Jitu

Answered On : Apr 13th, 2007

Hi all,

Can we use like this..

echo $string | tr " " "n"

Here the output will be something like this

One
Two
Three


if the value of string="One Two Three"

Please make me clear if i am wrong..i am a new baby to shell scripting. :-)
Thanks,
Jitu

  
Login to rate this answer.
Vishwajeet

Answered On : Jun 27th, 2007

Hi The answer is excellent but I like to modify it a little, only the command
string="one two three"tab=($string)$ echo ${tab[0]}one ${tab[1]}two ${tab[2]}three it self is enough to ptint "one two three", no more echos' are required.

  
Login to rate this answer.
amangautam80

Answered On : May 31st, 2008

View all answers by amangautam80

input="one two three"
echo $input | tr " " "n"

output would be like :

one
two
three

one of my friend gave this answer but in place of "n" he gave "n" only which will gave
output like :

onentwonthree

thanks

  
Login to rate this answer.
amangautam80

Answered On : May 31st, 2008

View all answers by amangautam80

Sorry typo error... in place on n we have to give "n"

  
Login to rate this answer.
Saroj.geek

Answered On : Nov 18th, 2008

View all answers by Saroj.geek

echo "one two three" | awk '{print $0 }'

$0willreturn the entire string as it'swithin quotes and will treat as one.

  
Login to rate this answer.
iamjogi

Answered On : Aug 23rd, 2009

View all answers by iamjogi

Try this:

echo "one two three" | xargs

the output is: one two three

  
Login to rate this answer.
abdas

Answered On : Jul 25th, 2011

View all answers by abdas

Code
  1. echo $1
  2. echo $2
  3. echo $3

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.