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
Related Articles:
- 2009/10/04 Get list of git repositories from command line.
- 2009/12/20 Know when you will type :q in your term instead of vi(m), the alias will chewed you out.
- 2009/12/18 Display a block of text with delineated by a start pattern and an end pattern
- 2009/12/07 View the newest xkcd comic.
- 2009/12/05 Insert a comment on command line for reminder
Authored By Amit Agarwal
Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.