I just started to learn unix...
and i needed to make a basic script.
i need to
- read a file (.txt)
- count the words of EVERY sentece
- sentences with odd number of words need to be converted into lowercase
sentences with even number of words need to be converted into uppercase
4.the translation needs to be written into a different file
I just can't sepperate the sentences and count the words of every sentance appart.
can someone help me
thx
We do not do homework for you. This is clearly homework.
-
use a while read record .. do ... done <inputfilename loop. This reads the file line by line.
-
tr will uppercase or lowercase a whole sentence.
-
wc -w will count the words.
Next, you get to show us what you've done with this.
I have not test it yet, because im still dowloading opensuse
could this work?
while read line
do
words = wc -w $line // counting the words of current line
If [ $\(\($words % 2\)\) -eq 0] Then // see if number of words is even
echo $File1 | tr '[a-z]' '[A-Z]' < $File2 // if even, change all to uppercase and copy to another file
Else
echo $File1 | tr '[A-Z]' '[a-z]' < $File2 // else; odd, change all to lowercase
Fi
Done < File1
small changes req...
while read line ;do
words = `echo "$line" |wc -w` // counting the words of current line
##OR###
###words=` echo "$line" |awk '{print NF}'`####
If [ $((words % 2)) �eq "0" ] ; then // see if number of words is even
echo "$line" | tr '[a-z]' '[A-Z]' >> outputfile.txt // if even, change all to uppercase and copy to another file
else
echo "$line" | tr '[A-Z]' '[a-z]' >> outputfile.txt // else; odd, change all to lowercase
fi
done < File1
That is pretty close. the < $File1 and < $File2 bits need to change direction.
And. What is the difference between > and >> ?
so if i would change the < into > it should be right?
> is overwriting the file
>> is adding 'content' to the file...
i think...
so it should be >>file2
edit: ow right... vidyadhar85 just said it... thx