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 the first line in a shell script?
Is scripting and coding the same thing?
How to print some text on to the screen?
Where cron file kept?
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.
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.
How to print all array elements and their respective indexes?
How can the contents of a file inside jar be read without extracting in a shell script?
When we login into our account which files are executed?
What is option in shell script?
What does $1 mean in bash?
How do I stop script errors?