【发布时间】:2011-01-16 12:15:21
【问题描述】:
我正在使用
sed -e "s/\*DIVIDER\*/$DIVIDER/g" 将*DIVIDER* 替换为用户指定的字符串,该字符串存储在$DIVIDER 中。问题是我希望他们能够指定转义字符作为分隔符,如 \n 或 \t。当我尝试这个时,我只是以字母 n 或 t 结尾,等等。
有人对如何做到这一点有任何想法吗?将不胜感激!
编辑:这是脚本的核心,我一定是遗漏了一些东西。
curl --silent "$URL" > tweets.txt
if [[ `cat tweets.txt` == *\<error\>* ]]; then
grep -E '(error>)' tweets.txt | \
sed -e 's/<error>//' -e 's/<\/error>//' |
sed -e 's/<[^>]*>//g' |
head $headarg | sed G | fmt
else
echo $REPLACE | awk '{gsub(".", "\\\\&");print}'
grep -E '(description>)' tweets.txt | \
sed -n '2,$p' | \
sed -e 's/<description>//' -e 's/<\/description>//' |
sed -e 's/<[^>]*>//g' |
sed -e 's/\&\;/\&/g' |
sed -e 's/\<\;/\</g' |
sed -e 's/\>\;/\>/g' |
sed -e 's/\"\;/\"/g' |
sed -e 's/\&....\;/\?/g' |
sed -e 's/\&.....\;/\?/g' |
sed -e 's/^ *//g' |
sed -e :a -e '$!N;s/\n/\*DIVIDER\*/;ta' | # Replace newlines with *divider*.
sed -e "s/\*DIVIDER\*/${DIVIDER//\\/\\\\}/g" | # Replace *DIVIDER* with the actual divider.
head $headarg | sed G
fi
一长串 sed 行正在替换来自 XML 源的字符,最后两个是应该用指定字符替换换行符的行。我知道用另一个换行符替换一个换行符似乎是多余的,但这是我能想到的让他们选择自己的分隔符的最简单方法。分隔符替换非常适合普通字符。
【问题讨论】:
-
您的脚本写得不好。很多不必要的 sed 步骤。显示您正在处理的输入文件,并显示您想要的输出。