linux sh : sed and awk's basic usage
发布日期:2021-06-30 22:17:04 浏览次数:2 分类:技术文章

本文共 8706 字,大约阅读时间需要 29 分钟。

前言

对字符串使用sed和awk,做了初步的实验。

实验目的,使参数可作为变量传递。

实验

# !/bin/bash# @file sh.test_case# @brief test function for other shell script. ../common/sh.const_definefunction func_test_sed() {    # --------------------------------------------------------------------------------    # sed option    # sed -e "script content"    # sed -f "script file name"    # --------------------------------------------------------------------------------    # --------------------------------------------------------------------------------    # 多行文本赋值到一个串变量的写法    # --------------------------------------------------------------------------------    local str_org="[\John Daggett, 341 King Road, Plymouth MA\n\Alice Ford, 22 East Broadway, Richmond VA\n\Orville Thomas, 11345 Oak Bridge Road, Tulsa OK\n\Terry Kalkas, 402 Lans Road, Beaver Falls PA\n\Eric Adams, 20 Post Road, Sudbury MA\n\Hubert Sims, 328A Brook Road, Roanoke VA\n\Amy Wilde, 334 Bayshore Pkwy, Mountain View CA\n\Sal Carpenter, 73 6th Street, Boston MA]"    local str_shell_script=""    local str_sed_result=""    echo -e "original string = $str_org"    echo $LINE80    # --------------------------------------------------------------------------------    # 书上ed条件, 正常的写法是用2个单引号围住    # --------------------------------------------------------------------------------    echo -e $str_org \    | sed 's/MA/lostspeed/'    echo $LINE80    # --------------------------------------------------------------------------------    # 可以用双引号来围住sed条件    # --------------------------------------------------------------------------------    echo -e $str_org \    | sed "s/MA/lostspeed/"    echo $LINE80    # --------------------------------------------------------------------------------    # sed条件可以为空,或者空条件用2个双引号(单引号)围住,不写条件    # --------------------------------------------------------------------------------    echo -e $str_org \    | sed ""    echo $LINE80    # --------------------------------------------------------------------------------    # sed一次执行多个指令, 用分号隔开多个指令    # --------------------------------------------------------------------------------    echo -e $str_org \    | sed "s/ MA/ road/; s/341/1000/; s/22/2000/;"    echo $LINE80    # --------------------------------------------------------------------------------    # 为了方便维护, 可以将sed脚本放到变量中    # --------------------------------------------------------------------------------    str_shell_script="s/MA/cat/"    echo -e $str_org | sed "$str_shell_script" # the var need around by " char, not by ' char    echo $LINE80    # --------------------------------------------------------------------------------    # save sed result to string    # --------------------------------------------------------------------------------    str_shell_script="s/MA/cat/"    str_sed_result=$(echo -e $str_org | sed "$str_shell_script")    echo "str_sed_result = $str_sed_result"    echo $LINE80    # --------------------------------------------------------------------------------    # sed : only search    # --------------------------------------------------------------------------------    str_shell_script='/MA/p' # p is print    str_sed_result=$(echo -e $str_org | sed -n "$str_shell_script") # -n is silent, don't print input org stream    echo "str_sed_result = $str_sed_result"    echo $LINE80}function func_test_awk() {    local str_org="[\John Daggett, 341 King Road, Plymouth MA\n\Alice Ford, 22 East Broadway, Richmond VA\n\Orville Thomas, 11345 Oak Bridge Road, Tulsa OK\n\Terry Kalkas, 402 Lans Road, Beaver Falls PA\n\Eric Adams, 20 Post Road, Sudbury MA\n\Hubert Sims, 328A Brook Road, Roanoke VA\n\Amy Wilde, 334 Bayshore Pkwy, Mountain View CA\n\Sal Carpenter, 73 6th Street, Boston MA]"    # use var on awk    # ref https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script    # default separate char is ' '    # --------------------------------------------------------------------------------    # $0 is all awk's input    # --------------------------------------------------------------------------------    echo -e "$str_org" | awk '{ print $0 }'    echo $LINE80    # --------------------------------------------------------------------------------    # $1 is input row's word.no.1    # $2 is input row's word.no.2     # --------------------------------------------------------------------------------    echo -e "$str_org" | awk '{ print $1 }'    echo $LINE80    echo -e "$str_org" | awk '{ print $2 }'    echo $LINE80    # --------------------------------------------------------------------------------    # awk's search    # --------------------------------------------------------------------------------    echo -e "$str_org" | awk ' /MA/ { print $1 }'    echo $LINE80    echo -e "$str_org" | awk '/Road/ { print $2 }'    echo $LINE80    local str_search_conditon='/Road/'    echo -e "$str_org" | awk '$str_search_conditon { print $1 }'    echo $LINE80    # --------------------------------------------------------------------------------    # awk's default split char is ' ', can change it to other char    # --------------------------------------------------------------------------------    str_search_conditon='/Road/'    local c_split_char=','    echo -e "$str_org" | awk -F$c_split_char '$str_search_conditon { print $1 }'    echo $LINE80    # --------------------------------------------------------------------------------    # execute multi awk command on one row    # --------------------------------------------------------------------------------    str_search_conditon='/Road/'    local c_split_char=','    echo -e "$str_org" | awk -F$c_split_char '$str_search_conditon { print $1; print $2; print $3; }'    echo $LINE80    # --------------------------------------------------------------------------------    # awk's command line format    # awk -option '/regex/ {command_1; command_2}'    # --------------------------------------------------------------------------------    # --------------------------------------------------------------------------------    # sed + awk    # --------------------------------------------------------------------------------    local str_sed_condition='/MA/p'    local str_awk_condition='/Road/'    local c_awk_split_char=','    echo    echo -e "this is sed + awk usage"    echo    str_sed_result=$(echo -e $str_org | sed -n "$str_sed_condition" | awk -F$c_awk_split_char '$str_awk_condition { print $1; print $2; print $3; }')    echo "str_sed_result = $str_sed_result"    echo $LINE80    # --------------------------------------------------------------------------------    # sed + awk code format    # --------------------------------------------------------------------------------    local str_sed_condition='/MA/p'    local str_awk_condition='/Road/'    local c_awk_split_char=','    echo    echo -e "this is sed + awk code format."    echo    str_sed_result=$(echo -e $str_org \    | sed -n "$str_sed_condition" \    | awk -F$c_awk_split_char \        '$str_awk_condition \        { \            print $1; \            print $2; \            print $3; \        }' \    )    echo "str_sed_result = $str_sed_result"    echo $LINE80    return 0}# --------------------------------------------------------------------------------# save some test case# --------------------------------------------------------------------------------function func_test_case() {    # func_test_sed    func_test_awk    return $G_ERR_CODE_OK}

2018-0217-1238

# 使用sed + awk 做一个有点意义的任务func_find_and_show_etc_passwd() {    local str_file_content=""    str_file_content=$(cat "/etc/passwd")    # print file /etc/passwd content    echo "str_file_content=[$str_file_content]"    echo $LINE80    # find row that contain "/bin/sh" and "/var/"    # print the user name    # sort name z to a    echo "$str_file_content" | sed -n "/\/bin\/sh/p" | sed -n "/\/var\//p" | awk -F: ' {print $1}' | sort -r    echo $LINE80    # --------------------------------------------------------------------------------    # 在脚本中使用变量,可以传参数给函数    # --------------------------------------------------------------------------------    local file_name="/etc/passwd"    local sed_filter1="/\/bin\/sh/p"    local sed_filter2="/\/var\//p"    local awk_split_char=":"    local run_result=""    run_result=$(echo "$(cat "$file_name")" | sed -n "$sed_filter1" | sed -n "$sed_filter2" | awk -F$awk_split_char ' {print $1}' | sort -r)    echo -e "$run_result"    echo $LINE80    # run result#   --------------------------------------------------------------------------------#   www-data#   uucp#   speech-dispatcher#   news#   man#   mail#   lp#   list#   libuuid#   irc#   gnats#   backup#   --------------------------------------------------------------------------------}

转载地址:https://lostspeed.blog.csdn.net/article/details/79331961 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:linux sh : bashdb的安装和初步使用
下一篇:linux sh : use local shell script library

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月21日 07时42分16秒