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
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 |
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 |
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 |
What is shell company all about?
What is the difference between a shell variable that is exported and the one that is not exported?
what is the difference between sh & bash shell?
How to add some content in any file at some desired location without using VI or some other editor in UNIX
What are scripts in psychology?
Differentiate between ‘ and ” quotes.
write a shell script that counts a number of unique word contained in the file and print them in alphabetical order line by line?
What is ms powershell?
How many prompts are available in a UNIX system?
What is the syntax of while loop in shell scripting?
what do u mean by $#,$* in unix programming?
Suppose you execute a command using exec, what will be the status of your current process in the shell?