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

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

0 Answers  


What is the best scripting language?

0 Answers  


What shell is bin sh?

0 Answers  


How would you replace the n character in a file with some xyz?

7 Answers  


How to declare functions in unix shell script?

4 Answers  






Devise a script that takes file name as arguement(which must present in the current directory)and locates from your home directory tree all thpath names of its links.Then mail the list to self.

0 Answers  


How do we delete all blank lines in a file?

0 Answers  


How do we create command aliases in a shell?

0 Answers  


What is basename in shell script?

0 Answers  


Create a bash shell script to sort and then uniq the file from the command line & store it to a new file and output the results to the screen. Name this script "sortAndUniq.sh"

5 Answers  


whta is the use of "exec" command?

1 Answers  


How do you print the output the same string which is typed in command line. how do you write the shell script or command for this. if i entered "Hello" in command line, it should print 'Hello', if i say "Hello Welcome", i should get the "Hello Welcome" as output.?

3 Answers   IBM, Symphony,


Categories