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 is the first line in a shell script?

0 Answers  


Is scripting and coding the same thing?

0 Answers  


How to print some text on to the screen?

2 Answers  


Where cron file kept?

2 Answers   Tech Mahindra,


Using set -A write a script to print the output of the ls command in 5 columns with two spaces between each column. Pretend that ls does not have multicolumn output.

0 Answers  






Hi, all Scripting professional. Q. I have written script1.sh and calling script2.sh in script1.sh file using bash shell as interpreter and my log in shell also bash shell.My code like Script1 #!/bin/bash echo "My script2 call" . script2.sh Here script2.sh file run successfully but when I have changed my interpreter bash to ksh like #!/bin/ksh Error are comming script2.sh command not found. Guid me how to call other script in our main script.

2 Answers  


How to print all array elements and their respective indexes?

0 Answers  


How can the contents of a file inside jar be read without extracting in a shell script?

0 Answers  


When we login into our account which files are executed?

3 Answers   Chip Quest,


What is option in shell script?

0 Answers  


What does $1 mean in bash?

0 Answers  


How do I stop script errors?

0 Answers  


Categories