首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

跪求shell脚本-sed grep 命令

2012-03-18 
跪求shell脚本高手指点--sed grep 命令有一文件 file.txt内容:aaa\(bb1aaa\(bb2ccc\ dd1ccc\ dd2有一脚本

跪求shell脚本高手指点--sed grep 命令
有一文件 file.txt内容:
aaa\(bb1
aaa\(bb2
ccc\ dd1
ccc\ dd2

有一脚本 file_sh.sh内容:
#!/bin/bash
file="file.txt"
grep_info_excap=`echo "$1" | sed -e 's/(/\\\\\\\\\\\(/g' | sed -e 's/ /\\\\\\\\\\\ /g'`
echo ${grep_info_excap}
echo "cat ${file} | grep "${grep_info_excap}" | tail -n 1"
for reader in `cat ${file} | grep "${grep_info_excap}" | tail -n 1`
do
  program_cmd="$program_cmd $reader"
done
echo ------------------------------------------- 
echo $program_cmd

命令1:
root@u-07:~#file_sh.sh c\ d
预期结果:
c\\\ d
cat file.txt | grep c\\\ d | tail -n 1
-------------------------------------------
ccc\ dd2

实际结果与之一致。

命令2:
root@u-07:~#file_sh.sh a\(b
a\\\(b
cat file.txt | grep a\\\(b | tail -n 1
-------------------------------------------
aaa\(bb2

实际:
a\\\(b
cat file.txt | grep a\\\(b | tail -n 1
grep: 不匹配的 ( 或 \(
-------------------------------------------

请求shell高手指点。。。谢谢!

[解决办法]
换成这样试试

grep_info_excap=`echo "$1" | sed -e 's/(/\\\\\\\\\(/g' -e 's/ /\\\\\\\\\\\ /g'`

热点排行