Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Thursday, August 7, 2014

Join odd and even numbered lines

sed:

sed '$!N;s/\n/,/'


awk:

awk 'NR%2==0 {print p","$0;} NR%2 {p=$0;}'


paste:

paste - -


--------------------------
Seems  I like paste most. To join 3 consecutive lines:

paste - - -

Wednesday, August 28, 2013

IO Redirection - Swapping stdout and stderr (Advanced)

% (sh myscript.sh 3>&2 2>&1 1>&3) 2>/dev/null
I'm stderr
% (sh myscript.sh 3>&2 2>&1 1>&3) >/dev/null 
I'm stdout

Thursday, March 7, 2013

brackets, parentheses, curly braces in BASH


In Bash, test and [ are builtins.
The double bracket enables additional functionality. For example, you can use && and || instead of-a and -o and there's a regular expression matching operator =~.
The braces, in addition to delimiting a variable name are used for parameter expansion so you can do things like:
  • Truncate the contents of a variable
    $ var="abcde"; echo ${var%d*}
    abc
  • Make substitutions similar to sed
    $ var="abcde"; echo ${var/de/12}
    abc12
  • Use a default value
    $ default="hello"; unset var; echo ${var:-$default}
    hello
  • and several more

Sunday, November 11, 2012

command sort on last column

As you know, the linux sort command is agile, which can sort on column using -k.

However, if we gonna sort on the last column and each row has different number of columns, we can still achieve that with the help of sed.

sed 's/^\(.\+\s\+\)\([0-9]\+ *\)$/\2 \1/'

Sunday, January 29, 2012

mac lion terminal doesn't source profile

I have encountered the problem that my MacBook Pro ~/.profile doesn't been sourced in the terminal.

The solution is simple:
bash first runs /etc/profile. /etc/profile (on Mac OS X 10.6.7) runs path_helper(8) which is where your default paths get set. After that it runs /etc/bashrc which doesn't do much. Once the default configuration is set, it moves on to the user login scripts.

bash looks in your home directory for .bash_profile, .bash_login, and .profile in that order. bash will run the first of these that it finds and stop looking.

So, if you have a .bash_profile in the home directory, the .profile will be ignored. In this case, just append a line in .bash_profile:

$cat "source ~/.profile" >> .bash_profile