sort listing numerically
but
ls -1 | sort -n
1.txt
2.txt
10.txt
:-)
column moving
Will take file and print the (whitespace separated? didn't check this) first two columns of each line in reversed order (first column 2 then column 1).
awk '{ print $2 $1 }' file
column selection
Print only second to third column of comma separated text in file.
replace text in multiple files
Perl offers the easiest way to do in-place-editing of text files:
perl -pi -e 's/tobereplaced/withthis/' filename
# for the cautious people: make backups before editing the file:
perl -pi'.bak' -e 's/tobereplaced/withthis/' filename
# therefore multiple files:
find . -name filename -exec perl -pi -e 's/pattern/bar/' {} ;# same with sed (in place edit, extended regex)
sed -i -r "s/tobereplaced/withthis/" filenameIt's possible to use other characters than / to delimit the patterns, e.g. pipe (|) or semicolon. This helps if the pattern itself contains slashes. Note that pattern is a regexp.
Thanks also to Stefan Tramm for this hints :-)
http://hacks.oreilly.com/pub/h/73Replacing several things in a file with the above shorthand can be time consuming as a new perl process is started for every file and every replacement. An elegant way is to use a script that contains all the replacement commands - for sure this works with perl, but I usually use sed for this:
# sed script 'deprecation-off.sed' could look like this:
s/org.eclipse.jdt.core.compiler.problem.deprecation=warning/org.eclipse.jdt.core.compiler.problem.deprecation=ignore/
s/org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsDeprecatedRef=enabled/org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsDeprecatedRef=disabled/# and a call to edit all eclipse settings files could look like this:
find ./*/.settings -name org.eclipse.jdt.core.prefs -print -exec sed -i -f deprecation-off.sed {} ;
bash string operations
Just a few but really useful:
# length of a string
${#string}# some examples
> echo ${USER}
stefan
> echo ${#USER}
6
> echo ${USER:2}
efan
> echo ${USER:2:1}
eSource:
http://tldp.org/LDP/abs/html/