This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
linux:basics [2018/07/17 13:15] mstraub |
linux:basics [2019/08/14 11:30] mstraub [Date & Time] |
||
---|---|---|---|
Line 1: | Line 1: | ||
This page covers basic usage of / working with Linux. | This page covers basic usage of / working with Linux. | ||
+ | |||
+ | A great resource are the man pages but also the [[https://www.gnu.org/software/coreutils/manual/html_node/index.html|official documentation of the GNU Coreutils]] | ||
Line 645: | Line 647: | ||
''date'' is a versatile tool for formatting dates / times and setting the system time ([[linux:installation#date_timezone|see also]]). | ''date'' is a versatile tool for formatting dates / times and setting the system time ([[linux:installation#date_timezone|see also]]). | ||
- | Get the current system time | + | Print the current time in different formats |
<code bash> | <code bash> | ||
- | date # print current system time (human readable) | + | date # default - a human readable format |
+ | date --rfc-3339=date # only the date - similar to ISO 8601 | ||
+ | date --rfc-3339=seconds # date and time - similar to ISO 8601 | ||
+ | date +%Y-%m-%dT%H%M # custom format useful for scripts, e.g. 2019-08-14T1129 | ||
</code> | </code> | ||
+ | |||
+ | Convert unix timestamps | ||
+ | <code bash> | ||
+ | date -d @1278923870 # print the unix timestamp in a human readable format | ||
+ | </code> | ||
+ | |||
+ | |||
+ | |||
====== Working with Text ====== | ====== Working with Text ====== | ||
Line 727: | Line 740: | ||
sort -u file.txt #sort file and only print unique lines (see also: ''uniq'') | sort -u file.txt #sort file and only print unique lines (see also: ''uniq'') | ||
</code> | </code> | ||
+ | |||
+ | == shuf == | ||
+ | |||
+ | Shuffle - the opposite of ''sort''. | ||
+ | People on StackOverflow claim it's faster than ''%%sort --random-sort%%''. | ||
== uniq == | == uniq == | ||
Line 780: | Line 798: | ||
</code> | </code> | ||
- | Two good trick to create readable regexes is to (1) escape the whole regex with single quotes and (2) use a different separator than ''/'' when appropriate. See for yourself in this example of replacing all backslashes in a line with slashes. | + | Two good tricks to create readable regexes are to (1) escape the whole regex with single quotes and (2) use a different separator than ''/'' when appropriate. See for yourself in this example of replacing all backslashes in a line with slashes. |
<code bash> | <code bash> | ||
Line 791: | Line 809: | ||
<code bash> | <code bash> | ||
sed -n 1~2p file.txt #only print odd lines (=print line 1 and then print each line at step 2) | sed -n 1~2p file.txt #only print odd lines (=print line 1 and then print each line at step 2) | ||
+ | </code> | ||
+ | |||
+ | By default ''sed'' regular expressions are limited, e.g. do not support matching groups. Use the switch ''-E'' to activate extended regular expressions and enjoy matching groups: | ||
+ | |||
+ | <code bash> | ||
+ | sed -E 's#(.*),(.*)#\1;\2#g' #replace a single comma with a semicolon | ||
+ | </code> | ||
+ | |||
+ | However, even with extended expressions ''sed'' does not support non-greedy expressions as it is possible with PCRE (perl compatible regular expressions). A good resort is to simply use ''perl'' itself: | ||
+ | |||
+ | <code bash> | ||
+ | perl -pe 's/.*thevalue="(.*?)".*/\1/g' file.txt | ||
</code> | </code> | ||