[ start | index | login ]
start > Virtual Brain > Linux > text operations

text operations

Created by stefan. Last edited by stefan, 317 days ago. Viewed 781 times. #10
[diff] [history] [edit] [rdf]
labels
attachments

sort listing numerically

ls -1
1.txt
10.txt
2.txt

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.

cut -f 2-3 -d , 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/" filename

It'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/73

Replacing 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} e

Source: >>http://tldp.org/LDP/abs/html/

no comments | post comment
search www.stefanrufer.ch
Google

Content

Me?


Blog Calendar

< February 2012 >
SunMonTueWedThuFriSat
1234
567891011
12131415161718
19202122232425
26272829

Weblog summary 2007, 2006, 2005, 2004


Content managed by SnipSnap

M

snipsnap.org | Copyright 2000-2002 Matthias L. Jugel and Stephan J. Schmidt