我想找到 data下磁盘使用数 expmount=data expdisk= df -h |sed -n "/$expmount/p" |awk '{print $5}'|awk -F "%" '{print $1}' 为什么总是抱错; 将语句改成 expdisk= df -h |sed -n "/\$expmount/p" |awk '{print $5}'|awk -F "%" '{print $1}' 就无输出了; 改成 expdisk= df -h |sed -n "${expmount}p" |awk '{print $5}'|awk -F "%" '{print $1}' 则把所有的全部输出了。
by magicyh - Shell - 2009-05-22 16:10:30 阅读(3633) 回复(10)
一个文本 bash-3.2$ cat file asdf asdf asdf 想在从第3行后插入文本 NEW_CONFIG="function RNC${RNCID}_Config { Serving=\"$RNCID\"; Drift=\"$RNCidDrift\"; IP_CP=\"$RNCIP\"; }" 执行 sed -i "3a\${NEW_CONFIG}" file 显示结果cat file asdf asdf asdf ${NEW_CONFIG} 也就是说这个${NEW_CONFIG}没有被展开,只是当做普通字符给添加到后面了。 哪位高人知道怎么能把${NEW_CONFIG}展开后添加到第三行后面呢?
假如aaa.txt文件内容如下: aaa bbb ccc ddd eee fff ggg 我现在要匹配ccc,并显示ccc所在行以下的所有行,即显示如下内容: ccc ddd eee fff ggg ------------------------------------------------- #export tmp="ccc" #sed -n "/$tmp/p" aaa.txt //这样匹配ccc那一行,是可以。 #sed -n "/$tmp/,$p" aaa.txt //但是我要匹配ccc及以下的所有行,这样就不行 该怎么写?
本帖最后由 爱斯基摩寂寞 于 2011-01-19 11:01 编辑 当sed引用的变量中有需要转义的字符怎么处理呢? b=18/Jan/2011:00:00:01 a=17/Jan/2011:19:26:17 echo $a a=17/Jan/2011:19:26:17 echo $b 18/Jan/2011:00:00:01 sed -n '/17\/Jan\/2011:19:26:17/,/18\/Jan\/2011:00:00:01/p' access.log |more 有结果返回 sed -n '/"$a"/,/"$b"/p' access.log |more 无结果返回 因为$a,$b的结果是时刻变化的 所以要用变量引入,为什么没...
本帖最后由 zriplj 于 2014-12-29 15:13 编辑 [code]cat test Hostname=192.168.1.19 #Hostname= ServerActive=192.168.33 #!/bin/bash ip=`ifconfig |awk -F'[ :]+' 'NR==2{print $4}'` echo $ip eval sed -r 's/(Hostname=)[0-9].*/\1$ip/' test 把配置文件Hostname=后面的IP换成当前系统的IP地址 这样写出错了。请问要怎么写呢?[/code]
大家好!我有一个问题向大家请教。 假如我有以下两个文本文件:[code][root@hp8 public_html]# grep 135.240.146.1 ~/.ssh/known_hosts 135.240.146.101 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA0vWo8DB2iGAGsWWw4TJNEC4pj8VhIxc8kRsg6kP5rR/kKQ3iP41vmv6n1JUygnTo9A7yX69UuU4NeNE= 135.240.146.102 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAo9U6FLjVJgpJ3iml8j0kI4YzCsou+okqm76QFAT9XYl41/rbBqYh85ecaK1wVBBkqcdxFclINravCbo= 135.240...
#!/bin/bash for((i=0;i<2;i++)) do echo $i sed -i 's/ClientId=[0-9]/ClientId='"$i"'/' client.ini done 用了这个办法 结果 client.ini 里面的 ClientId=$i 很奇怪 按CU得办法无法解决 请大拿出手帮忙
一个简单的shell,想对一个文件中的特定行,进行替换 #!/usr/bin/ksh total=20 i=1 while [ i -lt $total ] do cat a.txt | sed -e ' ${i} s/.*/xxxxxxxxxxxxxxxx/' > b.txt if [ $? -ne 0 ] then echo "failed to replace" exit -1 fi mv b.txt a.txt let i=i+10 done 在sed引用shell中的变量的时候失败,而单独 cat a.txt | sed -e ' 10s/.*/xxxxxxxxxxxxxxx/' 是可以的。 请问,如何在sed中引用shell中的变量啊?
我写了一个简单的shell script,目的是想对下面的文件进行资料处理: 文件内容如下: name item sum A aaa 100 B bbb 100 C ccc 100 Script如下: #Calculate the rows declare -i ROW=`cat $DIR/reim |wc -l` #把每个人的项目和金额列出来,并通过邮件通知给每个人。 for ((i=2;i<=$ROW;i=i+1)) do NAME=`cat $DIR/reim | awk '{print $1}' | sed -n '$ip'` ITEM=`cat...
我在shell中定义变量: [code]$LOG=/app/app.log[/code] 并执行如下查找替换 [code]sed -n 's/.*/&$LOG\/gp' file [/code] file中内容为 [code]1 2[/code] 目标查找结果为: [code]1/app/app.log 2/app/app.log[/code] 实际结果为: [code]1$LOG 2$LOG[/code] 因此想知道如何在shell的sed替换中进行变量引用。谢谢! .