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 are the various stages of a linux process it passes through?
What is the best scripting language?
What shell is bin sh?
How would you replace the n character in a file with some xyz?
How to declare functions in unix shell script?
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.
How do we delete all blank lines in a file?
How do we create command aliases in a shell?
What is basename in shell script?
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"
whta is the use of "exec" command?
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.?