Pages

Showing posts with label Utilities. Show all posts
Showing posts with label Utilities. Show all posts

Wednesday, 19 September 2012

Date

Date to epoch

The current date and time

 [root@localhost scripts]# date
Wed Sep 19 11:13:38 PDT 2012


The current date in terms of epoch. The --date="Wed Sep 19 11:13:38 PDT 2012" is copied from the output of date command.

[root@localhost scripts]# date --date="Wed Sep 19 11:13:38 PDT 2012" +%s
1348078418


Other acceptable format

[root@localhost scripts]# date --date="2012-09-19 11:13:38" +%s
1348078418


[root@localhost scripts]# date -d "2012-09-19 11:13:38" +%s
1348078418

If the time is omitted,  the time is implied as 00:00:00

[root@localhost scripts]# date --date="2012-09-19" +%s
1348038000


If the date is omitted, it is assumed to be the current date.

[root@localhost scripts]# date --date="10:57:04" +%s
1348077424

Add a number of seconds to a certain date in "YYYY-MM-DD HH:MM:SS"

[root@localhost scripts]# date -d  "2012-09-19 00:00:10 40 seconds"
Wed Sep 19 00:00:50 PDT 2012

[root@localhost scripts]# date -d  "2012-09-19 00:00:10 40 seconds" +%s
1348038050

Epoch to Date

Epoch time is the number of second from 1970-01-01 00:00:00 UTC
[root@localhost scripts]# date -d "1970-01-01 UTC 00:00:00 1348038050 seconds"
Wed Sep 19 00:00:50 PDT 2012



Todo: date diff, Timezone, Convert time for different TZ

Tuesday, 11 September 2012

Sorting and Grep

Read a big file line by line and do some grep

cat files.txt | xargs grep keyword

Appending a list of files to one file using xargs command

cat filelist | xargs cat >> bigfile.dat

Appending the content of a list of files to one file using xargs command
cat /tmp/filelist.txt | xargs cat |grep " ERROR] Failed path" >> /tmp/ed_search.err

Cut extract multiple columns
e.g Get the first column

/etc/passwd
root:x:0:0:root:/root:/bin/bash

cut -d":" -f1 /etc/passwd
root

Find out and sort the folder according to size

du --max-depth=1 /home/ | sort -nr

To sort the file on the third field (serial number) in reverse order and save the results in sorted.data, use this command:

sort -r +2 -3 company.data > sorted.data
Mel Ancholie 636496 Research
Jan Itorre 406378 Sales
Ed Jucacion 396082 Sales
Jim Nasium 031762 Marketing


Sort by field 5 numerically
$ ls -al | sort -n -k5



Cut out column 9 where the fields are separated by one or more spaces

[root@localhost locallog]# ll -r ed_search.*.log | awk -F" " '{print "/path/"$9}' >> /tmp/filelist.txt
[root@localhost locallog]# cat /tmp/filelist.txt
/path/ed_search.8.log
/path/ed_search.7.log
/path/ed_search.6.log
/path/ed_search.5.log
/path/ed_search.4.log
/path/ed_search.3.log
/path/ed_search.2.log
/path/ed_search.1.log



Eliminlate duplicate lines, sort before uniq
sort /tmp/ed_search_err_list.txt | uniq | wc -l

grep line below and above
grep -A 1 word file.txt --> Line and one line after
grep -B 1 word file.txt --> Line and one line before
grep -C 1 word file.txt --> One line before and after


grep "string" file , how to get the line number also?

grep -n "string" file

Range of characters from a string

The first parameter after the variable describes the starting position from zero inclusively.
The second parameter describes the number of character from the starting position.
[root@localhost scripts]# d=12345678
[root@localhost scripts]# echo ${d:3:2}
45
[root@localhost scripts]# echo ${d:0:2}
12


Return the number of character in a string

[root@localhost scripts]# d=12345678
[root@localhost scripts]# echo ${#d}
8