linux基础(day30)

awk复习

awk 中使用外部shell变量

  • awk 中使用外部shell变量
A=44echo "ABCD" | awk -v GET_A=$A ’{print GET_A}’
说明:-v选项用于定义参数,这里表示将变量A的值赋予GET_A。
有多少个变量须要赋值,就须要多少个-v选项。与之等价的:应用于脚本中:

#! /bin/bash
sort -n filename |awk -F ':' '{print $1}'|uniq >id.txt
for id in `cat id.txt`; do
        echo "[$id]"
        awk -v id2=$id -F ':' '$1==id2 {print $2}' filename  // 另外的方式为: awk -F ':' '$1=="'id'" {print $2}' filename  
done
附件:
cat filename
1111111:13443253456
2222222:13211222122
1111111:13643543544
3333333:12341243123
2222222:12123123123

运行脚本后结果为:
[1111111]
13443253456
13643543544
[2222222]
13211222122
12123123123
[3333333]
12341243123


思路:
先建立一个后缀为 .sh 的文件,而后将内容,添加进入,并将其中failname改成你建立的 后缀为 .sh 的文件
而后去执行shell文件——>sh 加 文件,便可看到结果

awk 合并一个文件

把一个文件多行链接成一行

awk中gsub函数的使用

  • awk 'gsub(/root/,"1111")' test.txt //把文件中全部的root替换为1111
  • awk -F ':' 'gsub(/root/,"lll",$1) {print $0}' test.txt //把$1 中的root替换为lll
    • 若这里不加 -F ':' 指定分隔符,获得的结果不一样
[root@hf-01 awk]# awk 'gsub(/root/,"1111")' test.txt    //把文件中全部的root替换为1111
1111x:0:0:1111:/1111:/bin/bash
operator:x:11:0:operator:/1111:/sbin/nologin
[root@hf-01 awk]# awk -F ':' 'gsub(/root/,"lll",$1) {print $0}' test.txt    //把$1 中的root替换为lll
lllx 0 0 root /root /bin/bash

在不加-F ':' 指定分隔符,获得的结果不一样
[root@hf-01 awk]# awk 'gsub(/root/,"AAA",$1) {print $0}' test.txt
AAAx:0:0:AAA:/AAA:/bin/bash
operator:x:11:0:operator:/AAA:/sbin/nologin

awk 截取指定多个域为一行

过滤两个或多个关键词

  • grep 或 egrep 或awk 过滤两个或多个关键词
grep -E '123|abc' filename  // 找出文件(filename)中包含123或者包含abc的行
egrep '123|abc' filename    //用egrep一样能够实现
awk '/123|abc/'  filename // awk 的实现方式

用awk生成如下结构文件

awk用print打印单引号

  • 方法一:html

  • head -3 |awk '{print "This is a '"'"'"$1}' test.txt //在前3行中,添加字符" This is a 'shell

    • 脱义的单引号字符 表达式为 ' " ' " '
  • 方法二:bash

  • head -2 test.txt |awk '{print"11'''" $1}' //在前2行中,添加字符11 '函数

    • 脱义单引号字符表达式为 " ' ' ' "
  • 添加双引号字符this

  • head -2 test.txt |awk '{print"aaa"" $1}' //在前2行中,添加字符aaa "code

    • 脱义双引号字符表达式为 " " "
单引号
方法一:
[root@localhost ~]# head -3 |awk '{print "This is a '"'"'"$1}' test.txt    //在前3行中,添加字符" This is a '
This is a 'root:x:0:0:root:/root:/bin/bash
This is a '&&&
This is a 'as***fsdf****

方法二
[root@localhost ~]# head -2 test.txt |awk '{print"11'\''" $1}'    //在前2行中,添加字符11 '
11'root:x:0:0:root:/root:/bin/bash
11'&&&

双引号
[root@localhost ~]# head -2 test.txt |awk '{print"aaa\"" $1}'    //在前2行中,添加字符aaa "
aaa"root:x:0:0:root:/root:/bin/bash
aaa"&&&

sed中添加单引号 '

  • head -2 test.txt|sed 's/(.*)/this '"'"'&/'g //在前2行中,添加字符11 '
    • 脱义单引号字符为 ' " ' " '
[root@localhost ~]# head -2 test.txt|sed 's/\(.*\)/11 '"'"'&/'g
11 'root:x:0:0:root:/root:/bin/bash
11 '&&&

总结

  • 在awk中脱义单引号字符有两种方法
      1. 脱义的单引号字符 表达式为 ' " ' " '
      1. 脱义单引号字符表达式为 " ' ' ' "
  • 在awk中脱义双引号字符方法
    • 脱义双引号字符表达式为 " " "

合并两个文件

  • paste 命令,将多个文件按照列队列进行合并
    • 格式:paste filename1 filename2 //将两个文件中相同的行合并到一行
    • -d 在两个文件链接处用一个 指定的字符 链接
  • paste 2.txt 3.txt //将2和3文件中相同的行合并到一行
[root@hf-01 ~]# cat 2.txt
a v c
1 f g
as 4 5a
dd && a.
1 2
[root@hf-01 ~]# cat 3.txt
aa a 6
13 f45
1 f fg 
abc 6a2 asf 4fa
cda
abc
1
[root@hf-01 ~]# paste 2.txt 3.txt    //将2和3文件中相同的行合并到一行
a v c	aa a 6
1 f g	13 f45
as 4 5a	1 f fg 
dd && a.	abc 6a2 asf 4fa
1 2	cda
	abc
	1
[root@hf-01 ~]# paste -d '+' 2.txt 3.txt    //把文件2和3合并到一行中,并用+ 号字符链接
a v c+aa a 6
1 f g+13 f45
as 4 5a+1 f fg 
dd && a.+abc 6a2 asf 4fa
1 2+cda
+abc
+1
[root@hf-01 ~]#

awk的参考教程

相关文章
相关标签/搜索