1、批量将某些文件写到文件夹中,写一个shell脚本
mvtopack.sh
#!/bin/bash
for f in`ls`
do
mkdir ${f}"urls"
mv ${f} ./${f}"urls"
done
2、当linux系统中安装了两个jdk时,需要转换使用,需要修改
cd ~
vi .bash_profile
修改其中的内容
3.排序
sort -n -r -k 2 -t : aa.txt
-n:数字排序,-r:逆排序,-k 2 第2列 , -t : 表示用冒号分割,aa.txt文件
On my machine (Mac bash prompt, GNU sort ...) this works:
sort -t ' ' -k 2,2 in.txt > out.txt
(A "real" tab between the quotes.)
To get the tab there I type CTRL-V, TAB (CTRL-V followed by TAB).
4、输出一个文件的前n行,输出到另一个文件
cat aa.txt | sed -n '1-10p' > bb.txt
5、将两个文件内容合并到一个地方:
cat aa.txt bb.txt > cc.txt
6、uniq
进行排序之后,您会发现有些行是重复的。有时候该重复信息是不需要的,可以将它除去以节省磁盘空间。不必对文本行进行排序,但是您应当记住 uniq
在读取行时会对它们进行比较并将只除去两个或更多的连续行。下面的示例说明了它实际上是如何工作的:
清单 1. 用 uniq 除去重复行
$ cat happybirthday.txt Happy Birthday to You! Happy Birthday to You! Happy Birthday Dear Tux! Happy Birthday to You! $ sort happybirthday.txt Happy Birthday Dear Tux! Happy Birthday to You! Happy Birthday to You! Happy Birthday to You! $ sort happybirthday.txt | uniq Happy Birthday Dear Tux! Happy Birthday to You! |
警告:请不要使用 uniq
或任何其它工具从包含财务或其它重要数据的文件中除去重复行。在这种情况下,重复行几乎总是表示同一金额的另一个交易,将它除去会给会计部造成许多困难。千万别这么干!
$ sort happybirthday.txt | uniq -u Happy Birthday Dear Tux! $ sort happybirthday.txt | uniq -d Happy Birthday to You! |
您还可以用 -c
选项从 uniq
中获取一些统计信息:3. 使用 -c 选项
$ sort happybirthday.txt | uniq -uc 1 Happy Birthday Dear Tux! $ sort happybirthday.txt | uniq -dc 3 Happy Birthday to You!
6、是文件类型的移到文件夹中
#!/bin/bash
for f in `ls`
do
if [ -f $f ];then
mkdir $f"g";
mv $f ./$f"g";
fi
done
7、切割文件,只要一列
cut -d: -f 1 /etc/passwd > /tmp/users
-d用来定义分隔符,默认为tab键,-f表示需要取得哪个字段
8、去重文件中的空行
grep -v '^$' input.txt >output.txt
9、显示数字,循环
#!/bin/bash
for(( i=2; i<11; ++i)) ; do
mkdir "data"$i;
done
echo "Using for loop method # 1... " for i in 1 2 3 4 5 6 7 8 9 10 do echo -n "$i " done
echo "Using while loop..." j=1 while [ $j -le 10 ] do echo -n "$j " j=$(( j + 1 )) # increase number by 1 done echo ""
ps -ax | grep nutch | awk '{print "kill " ,$1}'
学习使用awk
#!/bin/bash #there is a bug in the command ,process number length is not certain,so "-f 1" is possible blank. #arr=`ps ax | grep 'java -jar manager.jar' | cut -d " " -f 1`; arr=`ps ax | awk '$0 ~/java -jar .*app.jar$/ && $0 !~ /grep / {print $1}'` for comm in $arr ;do echo $comm; `kill -9 $comm`; done;
cat monitor_net.txt | grep eth0 | awk '{if($1>"08:29:00" && $1<"08:52:41"){reckb=$6/1024; if(reckb > 300) print reckb}}' >140_thread_net_result.txt
sleep
#!/bin/bash
a=`ls | wc -l`
echo $a;
if [ $a -gt 1 ];then
sleep 10s;
echo "test";
else
echo "test .....";
fi
a=`date --date='yesterday' +%Y%m%d`;
批量处理的脚本,只要肯想,一定有解决办法:
#!/bin/bash
k=1;
v=ww_info_20120801
r=ww_info_20120801_done
for((i=1;i<43153;++i));do
a=`head -n $i $v | tail -n 1 | cut -f 1`
j=`expr $i + 1`
b=`head -n $j $v | tail -n 1 | cut -f 1`
if [ $k == 1 ];then
`head -n $i $v | tail -n 1 | awk '{print "1"$0}' >> $r`
else
`head -n $i $v | tail -n 1 | awk '{print "2"$0}' >> $r`
fi
if [ $a -gt $b ];then
k=`expr $k + 1`
k=$(($k%2))
fi;
done
#!/bin/bash
cd /usr/xxx/data/data1_head_Result_history
a=`ls -al -t | grep "re" | head -n 277 | awk '{print $9}'`;
j=0
for i in $a; do
j=`expr $j + 1`
echo $j;
`mv $i /usr/xxx/data/houchuli/`
done
正确使用expr,如果expr的参数有非数字,就会报 语法错误!!!!!!!!
#!/bin/bash
sum=0;
for a in `ls`; do
b=`wc -l ${a} | cut -f 1 -d " "`;
echo $b;
sum=`expr $sum + $b`;
echo $sum;
done;
linux下使用dhcp,dsl联网
系统默认是dhcp,在/etc/network/interfaces 中的内容是:
auto lo
iface lo inet loopback
#dhcp链接网络
auto eth0
iface eth0 inet dhcp
#dsl链接网络
auto dsl-provider
iface dsl-provider inet ppp
pre-up /sbin/ifconfig eth0 up # line maintained by pppoeconf
provider dsl-provider
从dsl断开连到dhcp时,执行:service networking restart
当从dhcp连到dsl时,执行:pppoeconf eth0
随机取样的代码
awk 'BEGIN{srand();while(i<10000){k=int(rand()*100000000);if(!(k in a)){a[k]++;i++}}}(NR in a){print $1}' urfile
很好用!
查看文件编码格式:
vim aa.txt
:set fieencoding