1.

 

time=`date +"%Y-%m-%d %H:%M:%S"`
ps aux | grep -v 'USER' >> /tmp/temp.txt
while read -r line 

do
declare -i CPUUsage=`echo $line | awk '{print $3}' | awk -F. ' {print $1}'`

done < /tmp/temp.txt

 

2.

 

while read line
do
   echo $line
done<A  
<A写在最后相当于给整个while do 语句加了一个约束条件,读取文件A里每行至文件尾结束

while read line<A
do
    echo $line
done
<A写在前面,整个while do语句就没有约束条件, 因为 read line<A这个始终为真
表示 不停地  读取A中的第一行,赋值给参数line,然后打印参数line的值.

 

3.

 

while true
do
     read line <myfile
     [ $? -ne 0 ] && exit
done

 

4.

 

exec 3<tt.log
while read -u3 t1 t2 t3 t4
do
echo $t2 $t4
rexec lshas$t1 "ls -lt $t3|awk '{print \$5,\$9}' "
done
exec 3<&-

 

5.

 

cat tt.log|while read t1 t2 t3 t4
do
echo $t2 $t4
rexec lshas$t1 "ls -lt $t3|awk '{print \$5,\$9}' "
done

 

6.

 

while read t1 t2 t3 t4
do
echo $t2 $t4
sed 's/.*/sed eat the line ( & )/'
done <tt.log

7.

 

cat >hello.sh <<\EOF

#!/bin/sh

echo "Hello World!"

EOF

8.

 

#!/bin/bash

export exp9temp="hello world"

sleep 30

exit 0

 

9.

 

shell 向父 shell 传递自己的变量: 

  1. 通过一个中间文件进行:

    #!/bin/bash
    
    (
     subvar="hello shell"
     echo "$subvar" > temp.txt
    )
    
    read pvar < temp.txt
    
    echo $pvar

 

10.

 

echo 456|read a
echo $a

 

 read a <temp.txt 
echo $a
hello shell

11.

 

a=1
let "a++"

 

12.

 

awk '!a[$0]++' a.txt 

相当于!a[$0]&&a[$0]=a[$0]+1{print $0}

重复的不见了。a[$0]就是以每行内容为index的一个hash表;由于执行了++,它的初值变成了0,而!0=1,1为真;如果行内容重复,它的值增加后进行!否运算变成假。

 

 

13.

cat file1: 
0011AAA 200.00 20050321 
0012BBB 300.00 20050621 
0013DDD 400.00 20050622 
0014FFF 500.00 20050401 

cat file2: 
I0011 11111 
I0012 22222 
I0014 55555 
I0013 66666 

规则:比较 file1的1-4字符 和 file2的2-5 字符,如果相同,将file2 的第二列 与 file1 合并 file3 

0011AAA 200.00 20050321 11111 
0012BBB 300.00 20050621 22222 
0013DDD 400.00 20050622 66666 
0014FFF 500.00 20050401 55555 

  1. awk 'NR==FNR{a[substr($1,2,5)]=$2}NR>FNR&&a[b=substr($1,1,4)]{print $0, a[b]}' file2 file1 >file3

 

 

14.

 

如果文件a中包含文件b,则将文件b的记录打印出来
http://bbs.chinaunix.net/forum/viewtopic.php?t=520411

文件a: 
10/05766798607,11/20050325191329,29/0.1,14/05766798607 
10/05767158557,11/20050325191329,29/0.08,14/05767158557 

文件b: 
05766798607 
05766798608 
05766798609 
通过文件a和文件b对比,导出这样的文件出来. 
10/05766798607,11/20050325191329,29/0.1,14/05766798607

  1. wk -F'[/,]' 'ARGIND==1{a[$0]}ARGIND>1{($2 in a);print $0}' b a

  2. awk -F'[/,]' 'NR==FNR{a[$0]}NR>FNR{($2 in a);print $0}' b a

 

 

15.

 

a.txt:
1,a
2,a
3,a
4,b
5,c
6,c
b.txt:
a,100
b,200
c,300
d,400
目标:联结得到
1,100
2,100
3,100
4,200
5,300
6,300
awk处理脚本: 
awk 'BEGIN{FS=OFS=","}NR==FNR{a[$1]=$2}NR>FNR{print $1,a[$2]}' b.txt a.txt
awk -F \' 'BEGIN{OFS=","}NR==FNR{a[$1]=$2,next}{print $1,a[$2]}' b.txt a.txt