Thursday, October 14, 2010

sorting lines with vim

For e.g.

test.txt : 
z
y
x
w
v
u

If I say ->
:1,4 sort

test.txt will become
w
x
y
z
u
v

:.,+3 sort
will sort from current line to current line + 3.

:1,$ sort u
will sort and remove duplicates

To sort in reverse,
sort!

PS: This functionality doesn't work with IntelliJ Idea's vim plugin

Shell sample commands

Need to create a list of all the unique package names explicitly imported
by your Java code? The following stores it in a file called "list."

grep ' ^import ' *. j ava |
sed -e' s/. *import *//' -e's/; . *$//' |
sort -u >list


(sed -e is used for giving multiple commands to sed)

Shell sample commands


Which Java files have not been modified in the last week?
find . -name ' *. java' -mtime +7 -print


Which Java files have been modified in the last week?
find . -name ' *. java' -mtime -7 -print

Finding all files newer than a given file

find all c files newer than the Makefile : 

find . -name ' *. c' -newer Makefile -print

Problems in running shell scripts on cygwin

I copied pasted this code : 
#!/usr/bin/sh
N=1
while test "$N" -le "10"
do
echo "Number $N"
  N=$[N+1]
done

in test.sh.

Then ran it like : 
./test.sh

To get : 
./test.sh: line 7: syntax error: unexpected end of file

Issue was that Windows/Dos files don't work well
as shell scripts, due to difference in new line formatting.

So, the solution is , dos2Unix convert command : 
d2u test.sh

And, that's all.
Run happily ever after.

extracting social security number from an xml file using grep and cut

file.txt
<field10>123-45-6789</field10>
<field10>123-45-6789</field10>
<field10>123-4-6789-</field10>

here is the regexp

([0-9]+-)+[0-9]+<
and the command is 
grep -o '\([0-9]\+-\)\+[0-9]\+<' file.txt | cut -d "<" -f 1

Wednesday, October 13, 2010

Blog Archive