2-Cent Tips
[2-cent Tip]: Counting your mail
Ben Okopnik [ben at linuxgazette.net]
Sat, 28 Aug 2010 13:13:02 -0400
A few minutes ago, I needed to count all the emails I had archived in my ~/Mail directory. A moment of thought, and:
grep -rc '^From ' Mail/*|awk -F: '{s+=$NF}END{print s}'
NOTES: Email headers start with a 'From ' at the beginning of the line, so each line that starts that way identifies a single email. 'grep -r' is recursive - i.e., also searches subdirectories. The output from 'grep' is a bunch of lines, each of which looks like 'file.txt:17', which says that 'file.txt' has 17 matches; therefore, we use 'awk' to split each line on colons - but since the filename itself could contain a colon, we need to grab the very last field. In 'awk', 'NF' is the count of fields in each line, and '$X' is the value of field X - so '$NF' is the value of the last field. Sum them up, print them out when it's all over, and presto - count of emails.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * https://LinuxGazette.NET *
[ Thread continues here (3 messages/3.70kB) ]
[2-cent Tip]: Renumbering files
Ben Okopnik [ben at linuxgazette.net]
Wed, 18 Aug 2010 20:01:52 -0400
This comes up occasionally: you have a numbered list of files and you need to reorganize them, say by moving all the numbers up two places to accomodate two more files being added to the "front of the queue", or inserted in the middle of it. Even if you're familiar with loops, the answers aren't quite as simple as "loop over the numbers, add 2, and rename": doing so would overwrite the third file with the first one, the fourth one with the second one, and so on. Ugh, what a mess!
Here's the general form of an approach that'll work well:
start=0 # Lowest number in the list end=10 # Highest number in the list incr=2 # The increment for n in $(seq $start $end|tac); do mv $n $(($n + $incr)); done
Since piping the list through 'tac' will invert it, we will now be renaming the files in reverse order - that is, 10->12, 9->11, 8->10, and so on - which will prevent the above collisions. Renaming files that have numbers as part of the name isn't much more difficult: given, say, 'File1xyz.txt' and so on, the loop body simply becomes
for n in $(seq $start $end|tac) do mv File${n}xyz.txt File$(($n + $incr))xyz.txt done
Bonus feature: if you have a list of numbers that goes over 9 (or 99), and you want it sorted numerically (rather than '1 10 11 12 2 3 ...'), just use 'printf' to format the second parameter:
for n in $(seq $start $end|tac) do mv $n $(printf '%02d' $(($n + $incr))) done
The '02' in the above will result in numbers that are always 2 digits long, by prefixing the single-digit ones with a zero. Obviously, this can be extended to whatever number of digits is desired - and 'ls' will now show a properly numerically-sorted list.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * https://LinuxGazette.NET *
[ Thread continues here (3 messages/3.93kB) ]
[2-cent Tip] typing command in bash shell with comfort
Mulyadi Santosa [mulyadi.santosa at gmail.com]
Sat, 7 Aug 2010 16:00:38 +0700
OK, sounds a bit confusing. Let's just say you are a kind of man (or woman, of course) who thinks that typing something like: sudo find / -xdev -type f -perm /06000 -mmin -60 | xargs ls -lt -c will be so much comfortable if it is done inside an editor...let's say...uhm...vim.
How to do that? simply press Ctrl-X, followed by Ctrl-E. And depending of the content of your EDITOR environment variable, bash will fire up that editor. Start typing, edit as neccessary, and save. And kazaaammm, that command is executed right away.
PS: Tribute to commandlinefu.com and https://www.catonmat.net/blog for this neat piece of trick!
-- regards,
Mulyadi Santosa Freelance Linux trainer and consultant
blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
[ Thread continues here (5 messages/4.88kB) ]
Share |