扩展:grep(egrep)、awkgrepgrep、egrep或awk过滤两个或多个关键词grep:[root@adai003 grep]# grep 'root/|daemon' passwdro
扩展:grep(egrep)、awk
grep
- grep、egrep或awk过滤两个或多个关键词
grep:[root@adai003 grep]# grep 'root/|daemon' passwdroot:x:0:0:roprot:/root:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologinegrep:[root@adai003 grep]# egrep 'root|daemon' passwdroot:x:0:0:roprot:/root:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologinawk:[root@adai003 grep]# awk '/root|daemon/' passwdroot:x:0:0:roprot:/root:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologin[root@adai003 grep]# awk '/root/ || /daemon/ {print}' passwdroot:x:0:0:roprot:/root:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologin
awk
- 用awk编写生成以下结构文件的程序
用awk编写生成以下结构文件的程序:1,1,0000000001,0000000001,0000000001,0000000001,0000000001,0000000001,20051001101012,2,0000000002,0000000002,0000000002,0000000002,0000000002,0000000002,2005100110101(最后列使用现在的时间,格式为YYYYMMDDHHMISS)各列的值应如下所示,每增加一行便加1,共500万行。
方法1:
[root@adai003 grep]# awk 'BEGIN{for(i=1;i<=5;i++)printf("%d,%d,%010d,%010d,%010d,%010d,%010d,%010d,%d/n",i,i,i,i,i,i,i,i,strftime("%Y%m%d%H%M"))}'1,1,0000000001,0000000001,0000000001,0000000001,0000000001,0000000001,2017070718032,2,0000000002,0000000002,0000000002,0000000002,0000000002,0000000002,2017070718033,3,0000000003,0000000003,0000000003,0000000003,0000000003,0000000003,2017070718034,4,0000000004,0000000004,0000000004,0000000004,0000000004,0000000004,2017070718035,5,0000000005,0000000005,0000000005,0000000005,0000000005,0000000005,201707071803解析:%d, %d, %010d, %010d, %010d, %010d, %010d, %010d, %d1 1 0000000001 0000000001 0000000001 0000000001 0000000001 0000000001 201407200104在此定义i为行数,如果要写多几行,就把这里的5改成需要的数字即可,例如:20 {for(i=1;i<=20;i++) %010d: 0000000001 刚好10个数字。
方法2:使用shell脚本
#! /bin/bashfor i in `seq 1 5` do n=`echo "$i"|awk '{print length($0)}'` export m=$[10-$n] export o=`perl -e '$a='0';$b=$a x $ENV{"m"};print $b;'` export j=$i p=`perl -e '$c=$ENV{"o"}.$ENV{"j"};print $c;'` echo "$i,$i,$p,$p,$p,$p,$p,$p,`date +%Y%m%d%H%M%S`" done注:其中用到了perl,所以脚本整体看起来比较啰嗦,希望能找到更好的解决办法。PS: shell 执行效率很低,so 该脚本运行时间会很漫长!或:#!/bin/bashfor i in `seq 1 5` do n=`echo $i|awk '{print length($0)}'` n_0=$[10-$n] c_0="" for j in `seq 1 $n_0` do c_0="$c_0"0"" done echo $i,$i,$c_0$i,$c_0$i,$c_0$i,$c_0$i, $c_0$i,$c_0$i,`date +%Y%m%d%H%M%S` done运行结果同上!
- awk用print打印单引号
[root@adai003 grep]# awk -F ':' '{print "This is a '"' "'" $1}' passwd |head -3This is a ' rootThis is a ' binThis is a ' daemon脱义单引号和空格!
注: 在awk中使用脱义字符‘/’是起不到作用的,如果想打印特殊字符,只能使用 '""' 这样的组合才可以。
这里自左至右为单引号、双引号、双引号、单引号其中两个单引号为一对,两个双引号为一对。想脱义$那就是'"$"'。
paste命令
.paste命令用于将多个文件按照列队列进行合并。
.语法: paste [options] [filename1] [filename2]
Options:
-d:指定文件内容之间的分隔符
-s:串列进行而非平行处理(水平显示)
[root@adai003 grep]# paste file1 file2 |cat1 a22 bb333 ccc4444 dddd333 ccc22 bb1 a[root@adai003 grep]# paste -d '+' file1 file2 |cat1+a22+bb333+ccc4444+dddd333+ccc22+bb1+a[root@adai003 grep]# paste -s file1 file2 |cat1 22 333 4444 333 22 1a bb ccc dddd ccc bb a