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

Showing Answers 1 - 29 of 29 Answers

Rishi Pahuja

  • Jun 6th, 2005
 

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

  Was this answer useful?  Yes

abhay

  • Jul 5th, 2005
 

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

akebono

  • Jul 27th, 2005
 

sed and awk 

  Was this answer useful?  Yes

rno

  • 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

  Was this answer useful?  Yes

atrain

  • Mar 20th, 2007
 

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

  Was this answer useful?  Yes

pranab kumar ghosh

  • Apr 4th, 2007
 

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

  Was this answer useful?  Yes

Jitu

  • 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

  Was this answer useful?  Yes

Vishwajeet

  • 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.

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions