开发者社区 问答 正文

awk 分割字符串,但是我只要以第一个符号分割:报错

例如:

某文件中存在下列sql语句

sql1=select name,age frome peop_info where 1=1

我想读取文件,然后用awk取得后面的sql值

 $awk -F "=" '{print $2}' 这样遇到有多个等号的就只取第一个等号后面,第二个等号前面的

如何只根据第一个等号分割?



 

展开
收起
kun坤 2020-06-06 23:51:52 1666 分享 版权
1 条回答
写回答
取消 提交回答
  • 用awk也行的

    ~$ echo "sql1=select name,age frome peop_info where 1=1" | awk -F'=' '{s="";for(i=2;i<=NF;i++)s=s""(i==NF?$i:$i"=");print s}'

    select name,age frome peop_info where 1=1
    ######
    localhost:~ macbook$ echo "sql1=select name,age frome peop_info where 1=1" | cut -d '=' -f 2-
    select name,age frome peop_info where 1=1
    localhost:~ macbook$
    选用合适的工具
    ######

    t="sql1=select name,age frome peop_info where 1=1"

    echo ${t/sql1=/}

    2020-06-06 23:51:57
    赞同 展开评论
问答分类:
问答地址: