日期:2014-05-16  浏览次数:20712 次

跪求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'`