shell — one liner to selectively change case

2010-01-07 2 min read bash

Today I had a hard time, I had a herculian task of converting the case of file to upper case. Well that’s not difficult :), I know. What made it difficult was the fact that not the whole file had to be converted but only selective lines containing the work important. Okay now that too is not so difficult, I thought. But again the file size was huge, it had some 9 million lines. So, I just thought of trying my skills of shell programming (dont have much of it anyway). So here’s what I did:

for i in `cat aka`; do  echo $i;  if [[ `echo $i|grep important` ]]; then echo $i|tr [:lower:] [:upper:] > aka; else echo $i > aka; fi; done

Okay let me try to explain this in most simple words (in algorithm)

for each line in file

{

print the line

if the line contains important

then

change the case using tr command and put it in another file

else

put it as is in another file

endif

}

But the above one is wrong the correct one should be :

for i in `cat aka`; do  echo $i;  if [[ `echo $i|grep important` ]]; then echo $i|tr [:lower:] [:upper:] » aka; else echo $i » aka; fi; done

comments powered by Disqus