shell — one liner to selectively change case

2010-01-07 214 words 2 mins read

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


author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it