Wednesday, February 12, 2014

Linux Util 6

I inadvertently deleted the previous contents of this post. Blogger sucks :( would not allow me to revert the contents. I lost one of my precious search on the nmap command.

1) To add a column of numbers and print the sum:
 
awk '{sum += $2} END {print sum}' temp 

2) To list the contents of a local directory when inside a ftp connection:

!ls

In words, exclamatory sign followed by the actual command.

3) If you are transferring a lot of files through mput or mget and do not have the patience to hit the key 'y', then while opening the ftp connection:

ftp -i hostname

Interestingly, I would have thought -i would enable interactive mode, here is just the reverse.

4) If you have to convert from Unix time stamp to human-readable date form and vice versa, the following helps:

date -d @915149280       (to get readable date format from epoch seconds)

date +%s -d"Thu Nov 1 12:50:00 2013"         (to get the unix timestamp in seconds from normal date format)

I literally stumbled upon the second command while reading a related question and its answer from SO. This answer was both simple and complicated in its own way. I thought I am better with vim search and replace. Well, I thought wrong. So, here is the next tip. Stack Overflow rocks!!

5) If you have text file with full human-readable date string sitting between the columns, then use vim to replace them all to epoch time using the following search string.

An example line looks like this:
Jey tty8 Thu Nov 1 12:50:57 2012 - Thu Nov 1 12:51:21 2012 (00:00)

Search command (in escape mode in vim):
:%s/\v\w+\s\w+\s\d+\s\d+:\d+:\d+\s\d+/\=system('date +%s -d"'.submatch(0).'" | tr -d "\n"')/g

After replacing:
(Jey tty8 1351788657 - 1351788681 (00:00)

As you can see, after the 'system' word, sits the command I explained in point 4 above. I learnt a lot of new things from this search regular expression.
a) use of the submatch(0): this matches what you went looking for in the first half of the expression
b) tr is used for translation. In this case, it is used to delete the trailing "\n" that comes with executing the system command
c) \s is for matching a single space. What was surprising was the "+" symbol after the characters 'w' and 'd'. Without them, the command does not work.

No comments:

Post a Comment