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

Answers were Sorted based on User's Feedback



If you have a string "one two three", which shell command would you use to extract the s..

Answer / swaroopa

Below methods will work. You could use either cut or awk

echo "read data"
read data

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

echo $data |awk -F" " '{print $1}'
echo $data |awk -F" " '{print $2}'
echo $data |awk -F" " '{print $3}'

Is This Answer Correct ?    9 Yes 0 No

If you have a string "one two three", which shell command would you use to extract the s..

Answer / rachana

Cut command

Is This Answer Correct ?    5 Yes 1 No

If you have a string "one two three", which shell command would you use to extract the s..

Answer / seshadri sethi

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

Is This Answer Correct ?    5 Yes 1 No

If you have a string "one two three", which shell command would you use to extract the s..

Answer / narendrasairam

Though cut command works, if the string is too long you cant
expect redundancy in the code. So, better to translate the
spaces first and then reading the lines.

echo "enter the string :"
read string

echo $string | tr " " "\n" | sed '/^$/d' > lines.out

while read line
do
echo $line
done < lines.out

Is This Answer Correct ?    3 Yes 1 No

If you have a string "one two three", which shell command would you use to extract the s..

Answer / asit pal

echo $a | tr " " "\n" | head -1
echo $a | tr " " "\n" | head -2
echo $a | tr " " "\n" | head -3

Is This Answer Correct ?    2 Yes 1 No

Post New Answer

More Shell Script Interview Questions

Hi Friends, I am currently Undergoing Course On Testing.I am Planning To Keep Fake Resume.Can any One tell me the ways to Prepare i.e, Real Time experience For Manual Testing. With Regards, Vikram

1 Answers  


Write the syntax for "if" conditionals in linux?

0 Answers  


What is another name for a bash shell script that you might see?

0 Answers  


How would you print just the 25th line in a file using smallest shell script?

4 Answers  


Why should we use shell scripts?

0 Answers  






Create a bash shell script that removes all files whose names end with a "~" from your home directory and subdirectories. Name this script "cleanup.sh"

6 Answers  


What is k shell?

0 Answers  


What is the meaning of $1 in shell script?

0 Answers  


What are the various stages of a linux process it passes through?

0 Answers  


What is the difference between break and continue commands?

0 Answers  


What is the lifespan of a variable inside a shell script?

0 Answers  


What are the disadvantages of shell scripting?

0 Answers  


Categories