tvb2727
September 10, 2010, 6:34am
1
I am trying to show each word on a separate line, but read in all the words on one command line. Below is the code I have right now:
read name
MyString=$name
name2=" "
echo $MyString | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \
while read char
do
if [ "$char" = "name2" ]
then
echo "$final"
echo "in here"
final=""
else
final=`echo $final $char`
fi
done
For some reason it is not working. What am I doing wrong?
Want to read in:
testing this program
and want to show on the screen
testing
this
program
Any help would be appreciated.
tr -s ' ' '\n' < inputfile
Try that
itslee
September 10, 2010, 6:57am
3
Try out this to replace space with new lines
echo $name | tr '\040' '\012'
tvb2727
September 10, 2010, 7:02am
4
itslee - i tried that and its good, but now I want to put each of those words into an array. So i can do some things on each of those strings passed etc.
tvb2727
September 10, 2010, 7:07am
6
its on a linux server. #!/bin/bash
zaxxon
September 10, 2010, 7:27am
7
#!/bin/bash
read -a ARR
# echo all elements of array ARR
echo ${ARR[*]}
# echo element no. 2 of array ARR (Index starts at 0)
echo ${ARR[1]}
# echo number of elements
echo ${#ARR[*]}
itslee
September 10, 2010, 7:36am
8
try this...
i=0
echo $name | tr '\040' '\012' | while read output
do
myval=$output
i=`expr $i + 1`
done
tvb2727
September 10, 2010, 7:50am
9
thanks for everyone's help. I got it working I think.