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 happens on a system call?
Why is shell scripting important?
What are the additional egrep symbols?
What is shell environment?
write a shell script to check the failed jobs?
How would you print just the 25th line in a file using smallest shell script?
How to get the last line from a file using just the terminal?
What is the significance of $#?
How does path variable work?
What is ms powershell?
There is a record with fields namely name,roll no.,salary,grade etc.Now,write a script to create a file with multiple records have same combination of fields but with unique roll numbers.The script should work for different names in the input file.
What is the way to do multilevel if-else's in shell scripting?