精华内容
下载资源
问答
  • sed替换变量

    2016-03-01 15:24:00
    今天在写脚本时用到了sed,我用sed替换xml文件中的变量。一般在sed 中替换都用单引号,如下边 sed -i ‘s/10/1000/g’ test.xml但是如果需要把1000改成变量,如sed -i ’s/10/$num/g‘ test.xml这样就不成功。 ...

    今天在写脚本时用到了sed,我用sed替换xml文件中的变量。一般在sed 中替换都用单引号,如下边

    sed -i ‘s/10/1000/g’ test.xml
    但是如果需要把1000改成变量,如
    sed -i ’s/10/$num/g‘ test.xml
    这样就不成功。

    此时需要把单引号改成双引号,如下边例子
    $num=1000
    sed -i "s/10/$num/g" test.xml

    转载于:https://www.cnblogs.com/weijing24/p/5231193.html

    展开全文
  • linux sed 替换 变量

    2016-11-24 19:08:25
    我想替换JOB_ID_DEFAULT为10   开始写法 sed 's/JOB_ID_DEFAULT/${job_id}/' test > test.ctl 不行, 后来发现外层改成双引号就可以了 sed "s/JOB_ID_DEFAULT/${job_id}/" test > ...

    job_id = 10

    我想替换JOB_ID_DEFAULT为10

     

    开始写法

    sed 's/JOB_ID_DEFAULT/${job_id}/' test > test.ctl

    不行, 后来发现外层改成双引号就可以了

    sed "s/JOB_ID_DEFAULT/${job_id}/" test > test.ctl

     

     

    http://blog.csdn.net/zxx2403/article/details/7245339

    展开全文
  • 1、在包含某个字符的上一行或者下一行插入内容: ...[root@RedHat test]# sed -i '/hello/i\\up' testfile [root@RedHat test]# cat testfile up hello [root@RedHat test]# sed -i '/hello/a\\dow...

    1、在包含某个字符的上一行或者下一行插入内容:

    [root@RedHat test]# cat testfile 
    hello
    [root@RedHat test]# sed -i '/hello/i\\up' testfile 
    [root@RedHat test]# cat testfile 
    up
    hello
    [root@RedHat test]# sed -i '/hello/a\\down' testfile 
    [root@RedHat test]# cat testfile 
    up
    hello
    down
    [root@RedHat test]#
    

    假如有两个关键字hello,那么在每一行上面或者下面都插入内容

    2、sed替换变量内容:
    替换普通字符串:
    sed -i ‘s/10/100/g’ testfile 替换成功
    替换变量:
    sed -i ‘s/10/$num/g’ testfile 替换失败

    需要把单引号换成双引号:
    sed -i “s/10/$num/g” testfile 替换成功

    展开全文
  • 但是假如我在shell中以变量的形式传递呢? 看下面的脚本 [root@centosTest freshexp]# more sedexptry.sh #!/bin/bash file=$1 #export file mapping=$2 #mapping config while IFS="|" read flag old new...

    首先假设我想要echo 一个string带有单引号怎么实现呢?

    [root@centosTest ~]# echo '1'
    1

    [root@centosTest ~]# echo "\'1\'"
    \'1\'

    [root@centosTest ~]# echo ''\''1'\'''
    '1'


    但是假如我在shell中以变量的形式传递呢?

    看下面的脚本

    [root@centosTest freshexp]# more sedexptry.sh 
    #!/bin/bash
    file=$1          #export file
    mapping=$2       #mapping config
    while IFS="|" read flag old new
    do
        echo "flag is "$flag
        echo $old
        echo $new
        sed s/"\($flag  ..\?\)  $old"/"\1  $new"/ $file > $file.tmp
    done<$mapping
    [root@centosTest freshexp]# more mapping.cfg 
    asselv|'1'|'11111'
    [root@centosTest freshexp]# sh -x sedexptry.sh test.txt mapping.cfg 
    + file=test.txt
    + mapping=mapping.cfg
    + IFS='|'
    + read flag old new
    + echo 'flag is asselv'
    flag is asselv
    + echo ''\''1'\'''
    '1'
    + echo ''\''11111'\'''
    '11111'
    + sed 's/\(asselv  ..\?\)  '\''1'\''/\1  '\''11111'\''/' test.txt
    + IFS='|'
    + read flag old new
    [root@centosTest freshexp]# 
    



    看到没,他的echo竟然是echo ''\''1'\'''

    然后在sed中竟然是sed 's/\(asselv  ..\?\)  '\''1'\''/\1  '\''11111'\''/' test.txt

    其实我想要的是        sed s/"\(asselv  ..\?\)  '1'"/"\1  '11111'"/ test.txt

    其实我的目的是想将文件

    +0000000000000060496.,+0000000000000009017.,20130101,"1","quarav","  loandt  >  '$elquar'",,"quarav"
    +0000000000000060498.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060499.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060500.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060501.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060502.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '5'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060503.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060504.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060505.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd 
     in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060506.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060507.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '5'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    

    中的asselv  =  '1'   替换为 asselv  =  '1003'


    在不能解决上面的单引号的问题的时候我想了另一种解决方法:

    [root@centosTest freshexp]# more sedexport.sh 
    #!/bin/bash
    file=$1          #export file
    mapping=$2       #mapping config
    while IFS="|" read flag old new
    do
        echo $flag
        echo $new
        sed s/"\($flag  ..\?  '\)$old"/"\1$new"/ $file > $file.tmp
        mv $file.tmp $file
    done<$mapping

    一开始运行的不错,但是后来发现了问题,很隐蔽,看一下运行之后的文件就能想到错误错在哪里

    [root@centosTest freshexp]# more test2.txt
    +0000000000000060496.,+0000000000000009017.,20130101,"1","quarav","  loandt  >  '$elquar'",,"quarav"
    +0000000000000060498.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '11111'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060499.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060500.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '11111003'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060501.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060502.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '55555'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060503.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '11111'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060504.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060505.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '11111003'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060506.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060507.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '55555'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    [root@centosTest freshexp]# ^C
    [root@centosTest freshexp]# 
    mapping文件如下

    [root@centosTest freshexp]# more mapping2.cfg 
    asselv|1|11111
    asselv|5|55555
    [root@centosTest freshexp]# 

    看来还是的精确生成如下的sed才行

    sed s/"\(asselv  ..\?\)  '1'"/"\1  '11111'"/ test.txt

    但是里面的单引号让我头疼不已!!!!





    ----一个小测试:

    [root@centosTest freshexp]# message="a b c"
    [root@centosTest freshexp]# echo $message 
    a b c
    [root@centosTest freshexp]# URL="http://1.1.1.1/1.php?message=MESSAGE&receiver=RECEIVER"
    [root@centosTest freshexp]# echo $URL
    http://1.1.1.1/1.php?message=MESSAGE&receiver=RECEIVER
    [root@centosTest freshexp]# echo $URL|sed “s@MESSAGE@$message@”|sed “s@RECEIVER@$receiver@”^C
    [root@centosTest freshexp]# ^C
    [root@centosTest freshexp]# echo $URL|sed "s@MESSAGE@$message@"
    http://1.1.1.1/1.php?message=a b c&receiver=RECEIVER
    [root@centosTest freshexp]# a="'1'"
    [root@centosTest freshexp]# echo $a
    '1'
    [root@centosTest freshexp]# b="sssss'1'ssss"
    [root@centosTest freshexp]# echo $b
    sssss'1'ssss
    [root@centosTest freshexp]# echo $b|sed "s/$a/4"
    sed:-e 表达式 #1,字符 7:未终止的“s”命令
    [root@centosTest freshexp]# echo $b|sed "s/$a/4/"
    sssss4ssss
    [root@centosTest freshexp]# aa="'2'"
    [root@centosTest freshexp]# echo $b|sed "s/$a/$aa/"
    sssss'2'ssss
    [root@centosTest freshexp]# 

    [root@centosTest ~]# message="a b c"
    [root@centosTest ~]# receiver="aa"
    [root@centosTest ~]# URL="http://1.1.1.1/1.php?message=MESSAGE&receiver=RECEIVER"
    [root@centosTest ~]# echo $URL|sed s@MESSAGE@"$message"@|sed s@RECEIVER@$receiver@
    http://1.1.1.1/1.php?message=a b c&receiver=aa
    [root@centosTest ~]# echo $URL|sed "s@MESSAGE@$message@;s@RECEIVER@$receiver@"
    http://1.1.1.1/1.php?message=a b c&receiver=aa



    [root@centosTest ~]# sed 'N;N;s/"//g;s/message=\(.*\)\nreceiver=\(.*\)\nURL=\(.*\)MESSAGE\(.*\)RECEIVER/\3\1\4\2/' a
    http://1.1.1.1/1.php?message=a b c&receiver=aa
    
    [root@centosTest ~]# more a
    message="a b c"
    receiver="aa"
    URL="http://1.1.1.1/1.php?message=MESSAGE&receiver=RECEIVER"
    
    [root@centosTest ~]# 





    莫名其妙的问题好像解决了,详细如下

    [root@centosTest freshexp]# sh -x sedexptry13.sh test2.txt mapping.cfg 
    + file=test2.txt
    + mapping=mapping.cfg
    + IFS='|'
    + read flag old new
    + echo 'flag is asselv'
    flag is asselv
    + echo ''\''1'\'''
    '1'
    + echo ''\''11111'\'''
    '11111'
    + sed 's/\(asselv  ..\?\)  '\''1'\''/\1  '\''11111'\''/' test2.txt
    + IFS='|'
    + read flag old new
    [root@centosTest freshexp]# more test2.txt.tmp 
    +0000000000000060496.,+0000000000000009017.,20130101,"1","quarav","  loandt  >  '$elquar'",,"quarav"
    +0000000000000060498.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '11111'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060499.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060500.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060501.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060502.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '5'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060503.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '11111'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060504.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060505.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060506.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060507.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '5'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    [root@centosTest freshexp]# more sedexptry13.sh 
    #!/bin/bash
    file=$1          #export file
    mapping=$2       #mapping config
    while IFS="|" read flag old new
    do
        echo "flag is "$flag
        echo $old
        echo $new
        #sed s/"\($flag  ..\?\)  "$old""/"\1  "$new""/ $file > $file.tmp
        sed "s/\($flag  ..\?\)  $old/\1  $new/" $file > $file.tmp 
    done<$mapping
    [root@centosTest freshexp]# 
    [root@centosTest freshexp]# more test2.txt
    +0000000000000060496.,+0000000000000009017.,20130101,"1","quarav","  loandt  >  '$elquar'",,"quarav"
    +0000000000000060498.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060499.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060500.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060501.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060502.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '5'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060503.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060504.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060505.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060506.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060507.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '5'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    [root@centosTest freshexp]# 
    


    现在正式重新测试:

    [root@centosTest freshexp]# cp sedexptry13.sh sedexptry14.sh
    [root@centosTest freshexp]# chmod 777 *
    [root@centosTest freshexp]# more sedexptry14.sh 
    #!/bin/bash
    file=$1          #export file
    mapping=$2       #mapping config
    while IFS="|" read flag old new
    do
        echo "flag is "$flag
        echo $old
        echo $new
        #sed s/"\($flag  ..\?\)  "$old""/"\1  "$new""/ $file > $file.tmp
        sed "s/\($flag  ..\?\)  $old/\1  $new/" $file > $file.tmp 
        mv $file.tmp $file
    done<$mapping
    [root@centosTest freshexp]# vi mapping.cfg 
    
    
    asselv|'1'|'11111'
    asselv|'5'|'55555'
    
    [root@centosTest freshexp]# more test2.txt
    +0000000000000060496.,+0000000000000009017.,20130101,"1","quarav","  loandt  >  '$elquar'",,"quarav"
    +0000000000000060498.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060499.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060500.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060501.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060502.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '5'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060503.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060504.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060505.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060506.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060507.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '5'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    
    [root@centosTest freshexp]# sh -x sedexptry14.sh test2.txt mapping.cfg 
    + file=test2.txt
    + mapping=mapping.cfg
    + IFS='|'
    + read flag old new
    + echo 'flag is asselv'
    flag is asselv
    + echo ''\''1'\'''
    '1'
    + echo ''\''11111'\'''
    '11111'
    + sed 's/\(asselv  ..\?\)  '\''1'\''/\1  '\''11111'\''/' test2.txt
    + mv test2.txt.tmp test2.txt
    + IFS='|'
    + read flag old new
    + echo 'flag is asselv'
    flag is asselv
    + echo ''\''5'\'''
    '5'
    + echo ''\''55555'\'''
    '55555'
    + sed 's/\(asselv  ..\?\)  '\''5'\''/\1  '\''55555'\''/' test2.txt
    + mv test2.txt.tmp test2.txt
    + IFS='|'
    + read flag old new
    
    [root@centosTest freshexp]# more test2.txt 
    +0000000000000060496.,+0000000000000009017.,20130101,"1","quarav","  loandt  >  '$elquar'",,"quarav"
    +0000000000000060498.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '11111'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060499.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060500.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060501.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060502.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '55555'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060503.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '11111'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060504.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060505.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060506.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060507.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '55555'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    [root@centosTest freshexp]# 
    

    接着执行:

    [root@centosTest freshexp]# sh -x sedexptry14.sh test2.txt mapping.cfg 
    + file=test2.txt
    + mapping=mapping.cfg
    + IFS='|'
    + read flag old new
    + echo 'flag is asselv'
    flag is asselv
    + echo ''\''1'\'''
    '1'
    + echo ''\''11111'\'''
    '11111'
    + sed 's/\(asselv  ..\?\)  '\''1'\''/\1  '\''11111'\''/' test2.txt
    + mv test2.txt.tmp test2.txt
    + IFS='|'
    + read flag old new
    + echo 'flag is asselv'
    flag is asselv
    + echo ''\''5'\'''
    '5'
    + echo ''\''55555'\'''
    '55555'
    + sed 's/\(asselv  ..\?\)  '\''5'\''/\1  '\''55555'\''/' test2.txt
    + mv test2.txt.tmp test2.txt
    + IFS='|'
    + read flag old new
    [root@centosTest freshexp]# more test2.txt 
    +0000000000000060496.,+0000000000000009017.,20130101,"1","quarav","  loandt  >  '$elquar'",,"quarav"
    +0000000000000060498.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '11111'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060499.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060500.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060501.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060502.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '55555'  and     fnpdcd  in  ('C01000000','C02000000','C03000000','C04000000')",,"lastbl"
    +0000000000000060503.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '11111'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060504.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '2'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060505.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '1003'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060506.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '4'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    +0000000000000060507.,+0000000000000009019.,20120101,"1","lastbl","  asselv  =  '55555'  and     fnpdcd  in  ('C05000000','C06000000','C07000000')",,"lastbl"
    [root@centosTest freshexp]# 

    OK,解决!!!!





    草稿:

        sed 's/"\($flag  ..\?\)  '$(echo $old)'"/"\1  '$(echo $new)'"/' $file > $file.tmp


    sed "s/\(asselv  ..\?  '\)1/\111111/" test.txt


    sed "s/\($flag  ..\?\)  '1'/\1  '11111'/" test.txt


    sed "s/\($flag  ..\?\)  $old/\1  $new/" test.txt



    #!/bin/bash
    file=$1          #export file
    mapping=$2       #mapping config
    while IFS="|" read sou tar
        do
        echo $sou
        echo $tar
        sed s/$sou/$tar/ $file > $file.temp
    done<$mapping



    "\(asselv  ..\?\)  '1'"   |    "\1  '11111'"


        


    "\\(asselv  ..\\?\\)  \'1\'"|"\\1  \'11111\'"






    "\(asselv  ..\?\)  '\''1'\''"-------------/"\1  '\''11111'\''"/' test.txt




    [db2inst1@scpa test]$ vi 
    + file=test.txt
    + mapping=mapping3.cfg
    + IFS='|'
    + read sou tar
    + sed 's/"\(asselv  ..\?\)  '\''1'\''"/"\1  '\''11111'\''"/' test.txt
    + IFS='|'
    + read sou tar
    [db2inst1@scpa test]$ more mapping3.cfg 
    "\\(asselv  ..\\?\\)  \'1\'"|"\\1  \'11111\'"
    [db2inst1@scpa test]$ 




    -----
    [db2inst1@scpa test]$ sh -x sedexporttry.sh test.txt mapping3.cfg 
    + file=test.txt
    + mapping=mapping3.cfg
    + IFS='|'
    + read sou tar
    + echo '"\(asselv' '..\?\)' ''\''1'\''"'
    "\(asselv ..\?\) '1'"
    + echo '"\1' ''\''11111'\''"'
    "\1 '11111'"
    + sed 's/"\(asselv' '..\?\)' ''\''1'\''"/"\1' ''\''11111'\''"/' test.txt
    sed:-e 表达式 #1,字符 11:unterminated `s' command
    + IFS='|'
    + read sou tar
    [db2inst1@scpa test]$ 






    "\\(asselv\ \ ..\\?\\)  \'1\'"|"\\1\ \ \'11111\'"








    sed 's/"\(asselv' '..\?\)' '----'\''1'\'---'"/"\1' ''\''11111'\''"/' test.txt










    + sed 's/"\(asselv' '..\?\)' ''\''1'\''"/"\1' ''\''11111'\''"/' test.txt
    sed:-e 表达式 #1,字符 11:unterminated `s' command
    + IFS='|'
    + read sou tar
    [db2inst1@scpa test]$ more mapping6.cfg 
    "\\(asselv\ \ ..\\?\\)  \'1\'"|"\\1\ \ \'11111\'"
    [db2inst1@scpa test]$ 




    sed "s/"\($flag  ..\?\)  $old"/"\1  $new"/g" $file > $file.tmp


    sed s/"\(asselv  ..\?\)  '1'"/"\1  '11111'"/ test.txt




    sed s/"asselv  =  '1'"/"asselv  =  '11111'"/ test.txt




    $s/\([Oo]ccur\)ence/\1rence/


    sed s/"\(asselv  .*\)  '1'"/"\1  '11111'"/ test.txt






    sed s/"\(asselv  ..?\)  '1'"/"\1  '11111'"/ test2.txt


    .\{m, n\}
    sed s/"\(asselv  .\{0, 2\}\)  '1'"/"\1  '11111'"/ test2.txt








    sed s/"[\(asselv  ..\)|\(asselv  .\)]  '1'"/"\1  '11111'"/ test.txt




    sed s/"\(asselv  ..\?\)  '1'"/"\1  '11111'"/ test.txt




    sed s/"\(asselv  ..\?\)  '1'"/"\1  '11111'"/ test.txt






    sed s/\(asselv  ..\?\)  '1'/\1  '11111'/ test.txt




    sed s/"\(asselv  .*\)  '1'"/"\1  '11111'"/ test.txt

    展开全文
  • sed替换-带变量

    2021-03-30 09:11:43
    sed普通替换 我们替换一个文件中的某个字符串,比如替换jimo里的wiki为haha: $ sed 's/wiki/haha/g' jimo hehe haha jimo haha haha 但是当我们用变量时如何替换呢? 带有变量替换 声明一个变量 v1 $ v1=lily $ ...
  • 通常,我们使用sed进行变量替换的时候,替换和被替换变量都是hard-coded的。例如: sed -n ‘/comm/p’ /tmp/test.log 如果我们用一变量var,它的值根据上下文变化 $ var=”comm”,定义了变量,那么我们在...
  • sed处理变量替换

    千次阅读 2014-07-02 17:24:00
    [root@server ~]# a="test1" [root@server ~]# b="2test" [root@server ~]# echo $a | ...即--> 格式为sed "s $变量1 $变量2 ",注意一定是双引号,这里以空格为分隔符,正常可以使用sed "s/$变量1/$变量2
  • sed替换文件中某一行的内容@TOC sed用变量替换文件中某一行的内容 其中,lineNum变量表示要替换第几行,num变量表示要替换的内容,这里我设置的是随机数。 试了好多种方法,这样写才能支持变量,重点是不要c后面的"...
  • 今天说一下sed变量替换的问题这个问题主要还是一个群友提问的开始我也没有做出来root@lp:~/pwb/tmp#echo$a 9 root@lp:~/pwb/tmp#sed's/1/$a/g' ^C root@lp:~/pwb/tmp#echo123|sed's/1/$a/g' $a23就像是这样带着变量...
  • sed 变量替换

    2012-08-07 16:04:26
    一般说来sed替换的语法是 sed 's/abc/xyz/g' s表示search,g表示gloabl全部替换, abc为被替换字符,xyz为要替换的字符,两边是单引号。如果牵涉变量名,则应为 sed 's/abc/‘“$url”’/g' 或者 sed "s/abc/$...
  • sed替换字符串(变量)

    2019-09-21 08:00:11
    sed “s/查找字段/替换字段/g”echo helloworld|sed ‘s/hello/world/g’sed 替换字符串以变量形式1、sed命令使用双引号的情况下,可以使用$var(变量)直接引用: echo | sed "s/^/$RANDOM.rmvb_/g" 13562.rmvb_...
  • sed 替换字符串以变量形式

    千次阅读 2019-01-02 13:10:36
    1、sed命令使用双引号的情况下,可以使用$var(变量)直接引用:  echo | sed "s/^/$RANDOM.rmvb_/g"  13562.rmvb_  2、sed命令使用单引号的情况下,可以使用'"$var"'引用(单引号,然后双...
  • Linux sed变量替换

    千次阅读 2011-04-09 20:23:00
    欲将 待处理文本 install_database.bak.rsp 中的所有 %orcl% 替换为 $ORACLE_SID的值,方法: sed -i "s/%orcl%/$ORACLE_SID/g" install_database.bak.rsp
  • shell命令sed替换文件内容

    千次阅读 2018-06-06 16:02:07
    sed修改文件ip地址,但改的内容是一个变量,直接修改是不是行的先修改ip地址等于xxxx,再用sed 替换 变量eavl sed -i 's/xxxx/$ip' filesed使用变量只能在替换时使用,用法如上所示...
  • cat sourcefile|sed "s:@PATH@:$userPath:g" &gt;temp   echo find /opt/timer/errorlog/ -name \*.txt -mtime -1 for TxtFile in $(find /opt/timer/errorlog/ -name \*.txt -mtime -1) do ...
  • sed 变量替换2012

    2015-03-19 11:06:39
    转自:...通常我们使用sed的时候使用单引号   $echo "aaaa" | sed -n 's/aaaa/bbbb/p' ...但是如果我们想使用变量替换呢?   b=bbbb $echo "aaaa" | sed

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 30,268
精华内容 12,107
关键字:

sed替换变量