Administrator
发布于 2023-07-03 / 35 阅读
0
0

Linux Process Text Tools

grep: 过滤文本内容
sed: 编辑文本内容
awk: 显示文本

Awk

refer to: https://www.man7.org/linux/man-pages/man1/awk.1p.html

awk — pattern scanning and processing language

DESCRIPTION

An awk program is a sequence of patterns and corresponding actions. When input is read that matches a pattern, the action associated with that pattern is carried out.

OPTIONS

-F sepstring
Define the input field separator.

OPERANDS

argument Either of the following two types of argument can be intermixed:

             file      A pathname of a file that contains the input
                       to be read, which is matched against the set
                       of patterns in the program. If no file
                       operands are specified, or if a file operand
                       is '-', the standard input shall be used.

工作中使用

awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理

awk工作流程是这样的:读入有'\n'换行符分割的一条记录,然后将记录按指定的域分隔符划分域,填充域,0则表示所有域,1表示第一个域,$n表示第n个域。

## 表示以“:”分割字符串,打印第一个
awk -F':' '{print $1}'

[root@VM-0-3-opencloudos ~]# echo "aa bb cc" | awk '{print $1}'
aa

找寻指定java程序的进程id:

ps ax | grep -i 'iot-manage' | grep java | grep -v grep | awk '{print $1}

获取目录名称

ORIGIN_FILE_NAME=$(ls -lt ${LOCAL_FILE_PATH}  | grep "^d" | head -1 | awk '{print $NF}')

上面的print $NF,表示打印出最后一列的数据

$NF 表示打印出等于总列数的那一列的数据,显而易见就是打印最后一列的数据

${} 获取到文件后缀名

${ }分别替换得到不同的值

# 是 去掉左边(键盘上#在 $ 的左边);

%是去掉右边(键盘上% 在$ 的右边);

单一符号是最小匹配;两个符号是最大匹配;

*是需要删除那边就放在哪边。
替换功能
${file#*/}删掉第一个 / 及其左边的字符串
${file##*/}删掉最后一个 / 及其左边的字符串
${file#*.}删掉第一个 . 及其左边的字符串
${file##*.}删掉最后一个 . 及其左边的字符串
${file%/*}删掉最后一个 / 及其右边的字符串
${file%%/*}删掉第一个 / 及其右边的字符串
${file%.*}删掉最后一个 . 及其右边的字符串
${file%%.*}删掉第一个 . 及其右边的字符串
# 后缀名
SUFFIX_NAME=$(echo "${UPLOAD_FILE_NAME#*.}")


评论