lprm -Pprintername -
to cancel all printing jobs
lpq -Pprintername
to view jobs
Monday, November 28, 2011
linux shell process filename from a text file
In my TA work I need to do the following:
Given a text file called names.txt, which contains all student ids:
aaa
bbb
ccc
ddd
Now I need to grep all files with a prefix matching one of the ids in names.txt, that is, I need to get
aaa.cpp
bbb.cpp
ccc.cpp
but the directory might contain a lot more other *.cpp
The first blessing is from grep
ls *.cpp | grep -f names.cpp > filenames.txt
gives the filenames that matching any entry in names.cpp
Now comes the hard part, how do you use those entries.
ls or find comes into mind but I don't know how to use them with input filename from a text file.
So it is awk.
awk '{ cmd="ls -l " $0; system(cmd) }' < filenames.txt
And that's it.
Given a text file called names.txt, which contains all student ids:
aaa
bbb
ccc
ddd
Now I need to grep all files with a prefix matching one of the ids in names.txt, that is, I need to get
aaa.cpp
bbb.cpp
ccc.cpp
but the directory might contain a lot more other *.cpp
The first blessing is from grep
ls *.cpp | grep -f names.cpp > filenames.txt
gives the filenames that matching any entry in names.cpp
Now comes the hard part, how do you use those entries.
ls or find comes into mind but I don't know how to use them with input filename from a text file.
So it is awk.
awk '{ cmd="ls -l " $0; system(cmd) }' < filenames.txt
And that's it.
Tuesday, November 22, 2011
the FKG inequality and facility location problem
Spent one day working on Sviridenko's paper on one inequality and finally see why it is
true.
Let y1,y2,...y_m be the probabilities that each facility open, sum of the y_i's is 1. Now
the expected distance is
dist=
c1*y1 + c2*y2*(1-y1) + c3*y3*(1-y1)(1-y2) + ... + cm*ym*(1-y1)...(1-y_m-1) + c_m+1*(1-
y1)...(1-ym)
and we want to show
dist <= (c1*y1+c2*y2+...+cm*ym)(1-1/e) + c_m+1 * 1/e
We know that y1+...+ym=1
Sviridenko noticed that
dist <= c1*(1-e^-y1) + c2*(e^-y1 - e^-(y1+y2)) + c3*(e^(-y1-y2)-e^(-y1-y2-y3)) + ... + cm*
(e^(-y1-...-y_m-1) - e^(-y1-...-ym)) + c_m+1*(1/e)=rhs
This is because c1<=c2<=...<=cm<=c_m+1
and the fraction of dist is always leading the rhs.
y1 >= 1-e^-y1
y1+y2(1-y1) >= 1-e^(-y1-y2)
Now we want to show rhs <= (c1*y1+c2*y2+...+cm*ym)(1-1/e) + c_m+1 * 1/e
and this is the part that took me a full day.
We actually use a similar idea. Notice that the sum of fractions are the same on both sides,
1-e^-y1 + e^-y1 - e^(-y1-y2) + ... + e^(-y1-y2-...-y_m-1) - e^(-y1-y2-...-y_m) = 1-e^-1
and
(y1+y2+...+ym)(1-1/e)=1-1/e
So we only need to show that the fraction of lhs is always leading.
1-e^-y1 >= y1(1-1/e)
1-e^-y1-y2 >= (y1+y2)(1-1/e)
...
1-e^-y1-y2-...-ym >= (y1+y2+...+ym)(1-1/e)
This can be proved by noticing the function f(x)=1-(e^-x)-x(1-1/e) evaluates to 0 at x=0 and
x=1, and in between it is always nonnegative. And y1, y1+y2, ... , y1+...+ym area all
between 0 and 1.
A slightly different proof due to Byrka uses FKG inequality
(u1*f1+u2*f2+...+um*fm)(u1*g1+u2*g2+...+um*gm) >= (u1+...+um)
(u1*f1*g1+u2*f2*g2+...+um*fm*gm)
assuming u1,u2,...,um >= 0 and f is increasing and g is decreasing.
Now we use
u1=y1 f1=c1 g1=1
u2=y2 f2=c2 g2=1-y1
u3=y3 f3=c3 g3=(1-y1)(1-y2)
The FKG inequality gives
(y1+...+ym)(y1*c1*1 + y2*c2*(1-y1) + ... + ym*cm*(1-y1)...(1-y_m-1))
<= (y1*c1+y2*c2+...+ym*cm)(y1*1+y2(1-y1)+...+ym(1-y1)(1-y2)...(1-y_m-1))
Notice this part (y1*1+y2(1-y1)+...+ym(1-y1)(1-y2)...(1-y_m-1)) = 1-(1-y1)...(1-ym) since
they are complementary events. lhs is one of y1,...,ym happen, rhs is one minus none happen.
Therefore
(y1*c1*1 + y2*c2*(1-y1) + ... + ym*cm*(1-y1)...(1-y_m-1))
<= (y1*c1+y2*c2+...+ym*cm)(1-(1-y1)(1-y2)...(1-ym)
Add (1-y1)...(1-ym)c_m+1 to both sides we have
expected dist <= (c1*y1+c2*y2+...+cm*ym)(1-(1-y1)...(1-ym)) + c_m+1(1-y1)...(1-ym)
<= (c1*y1+...+cm*ym)(1-1/e) + c_m+1*1/e since c1<=c2<=...<=cm<=c_m+1
Monday, November 21, 2011
TC SRM 524
p250
notice that if n is prime, then do it 2 times, one with n-1 and one with 1. if n is not prime, then do it only 1 time. There are two special cases, when n is 1, do it only 1 time, and when n is 3, need three times. The reason is that any prime above 3 has n-1 being nonprime while 3-1=2 is prime.
p500: unsolved
if all c[i]>0 or all c[i]<0, then answer is infinity. some upper bound, length <= lcm(c+, c-) since the smallest abs c[i]>0 and c[i]<0, their lcm will be both >0 and <0, which is impossible.
some lower bound is min(abs(c[i]))-1, then no restrictions apply and the sequence is always good.
p1000:
notice that if n is prime, then do it 2 times, one with n-1 and one with 1. if n is not prime, then do it only 1 time. There are two special cases, when n is 1, do it only 1 time, and when n is 3, need three times. The reason is that any prime above 3 has n-1 being nonprime while 3-1=2 is prime.
p500: unsolved
if all c[i]>0 or all c[i]<0, then answer is infinity. some upper bound, length <= lcm(c+, c-) since the smallest abs c[i]>0 and c[i]<0, their lcm will be both >0 and <0, which is impossible.
some lower bound is min(abs(c[i]))-1, then no restrictions apply and the sequence is always good.
p1000:
Friday, November 4, 2011
dell cannot display this video mode
Found an old dell dimension 4500 from some corner of the lab and decide to put up a Windows machine with office because the department machines are all linux and occasionally I do need windows and office to communicate with the rest of the world.
After installing xp3 and all the updates, and sophos, the monitor failed to display after reboot. Type the title into google search pops a lot webpages, so other people are seeing this as well.
The following works for me:
1. reboot machine and into safe mode.
2. in safe mode, control panel->computer administrative->device driver->uninstall display adapter driver
3. reboot into normal mode
After installing xp3 and all the updates, and sophos, the monitor failed to display after reboot. Type the title into google search pops a lot webpages, so other people are seeing this as well.
The following works for me:
1. reboot machine and into safe mode.
2. in safe mode, control panel->computer administrative->device driver->uninstall display adapter driver
3. reboot into normal mode
C traps and pitfalls by Andrew Koenig
int a[] = { 1, 2, 3, };
It is legal to have a trailing comma after the last literal, namely 3 here.
because a[i] is the same as *(a+i), it is also the same as i[a], surprising, but true.
It is legal to have a trailing comma after the last literal, namely 3 here.
because a[i] is the same as *(a+i), it is also the same as i[a], surprising, but true.
Wednesday, November 2, 2011
how to print man page from linux
Some time it is necessary to print a Linux man or info page. To print a ls command man page type the following command:
$ man ls | col -b | lpr -P hp1_floor2
Where,
col -b : To format man page output, it will not show any special character such as backspaces
lpr -P hp1_floor2 : hp1_floor2 is the printer name of the Linux/UNIX printer queue
To print the man page with effects such as italic and bold fonts on the HP Laser Jet printer, enter:
$ zcat /usr/share/man/man1/ls.1.gz | groff -man -Tps | lpr -P hp1_floor2
You can save man page as a ps file, enter:
zcat /usr/share/man/man1/ls.1.gz | groff -man -Tps >top.ps
Where,
zcat : Open compressed /usr/share/man/man1/ls.1.gz file
groff : Go through groff document formatting system, which is responsible for formatting of man page with effects such as italic and bold fonts on both screen and printer
lpr -P hp1_floor2 : hp1_floor2 is the printer name of the Linux/UNIX printer queue
Here is a shell script that we have installed on all Linux workstations, it saves man page in a text file and later user can print it using lpr command.
#!/bin/bash
# Linux shell script to save man pages to text file
# so that later user can print them
# Created by nixcraft, under GPL v2.0 or above.
# ----------------------------------------------------------------------
CMDS="$@"
OUTDIR="/tmp/$USER.man"
[ ! -d "$OUTDIR" ] && mkdir -p "$OUTDIR" :
for i in $CMDS
do
man "${i}" col -b > "$OUTDIR/${i}.txt"
done
echo "**********************************************************"
echo "All man pages saved to $OUTDIR directory"
echo "Just goto \"$OUTDIR\" directory and type the following command
to print man page(s):"
echo "lpr *.txt"
echo "**********************************************************"
exit 0
Just type the script-name followed by Linux commands, for example to print a man page of ls and bash type
$ saveman "ls bash"
$ man ls | col -b | lpr -P hp1_floor2
Where,
col -b : To format man page output, it will not show any special character such as backspaces
lpr -P hp1_floor2 : hp1_floor2 is the printer name of the Linux/UNIX printer queue
To print the man page with effects such as italic and bold fonts on the HP Laser Jet printer, enter:
$ zcat /usr/share/man/man1/ls.1.gz | groff -man -Tps | lpr -P hp1_floor2
You can save man page as a ps file, enter:
zcat /usr/share/man/man1/ls.1.gz | groff -man -Tps >top.ps
Where,
zcat : Open compressed /usr/share/man/man1/ls.1.gz file
groff : Go through groff document formatting system, which is responsible for formatting of man page with effects such as italic and bold fonts on both screen and printer
lpr -P hp1_floor2 : hp1_floor2 is the printer name of the Linux/UNIX printer queue
Here is a shell script that we have installed on all Linux workstations, it saves man page in a text file and later user can print it using lpr command.
#!/bin/bash
# Linux shell script to save man pages to text file
# so that later user can print them
# Created by nixcraft
# ----------------------------------------------------------------------
CMDS="$@"
OUTDIR="/tmp/$USER.man"
[ ! -d "$OUTDIR" ] && mkdir -p "$OUTDIR" :
for i in $CMDS
do
man "${i}" col -b > "$OUTDIR/${i}.txt"
done
echo "**********************************************************"
echo "All man pages saved to $OUTDIR directory"
echo "Just goto \"$OUTDIR\" directory and type the following command
to print man page(s):"
echo "lpr *.txt"
echo "**********************************************************"
exit 0
Just type the script-name followed by Linux commands, for example to print a man page of ls and bash type
$ saveman "ls bash"
Subscribe to:
Posts (Atom)