使用sed为korn中的变量内容添加前缀shell
Using sed to add a prefix to variable content in korn shell
我试图远离所有 运行 时间临时文件。我能够用 运行 时间变量替换所有这些,但是当我将命令与变量一起使用时,一个命令给我带来了困难。
rimco 内容:包含文件位置
/home/bim
/home/ram
/home/gps
/home/hdr
/home/pal
bim内容:包含一组数字
28800,1536,14400,768
11100,12312,902,321
3044,1536,1290,334
3044,1536,1290,334
11100,12312,902,321
28800,1536,14400,768
目标是在找到我要查找的号码时为变量添加前缀 $output
。该行存储在 $output
中,前缀为 $dte
dte="available"
for f in $(cat rimco)
do
out=`grep 768 $f`
output+=`echo -e "\n$out"| sed "s/^/$dte,/"`
done
嗯,结果不好看,dte内容,到处都是
available,
available,28800,1536,14400,768
available,28800,1536,14400,768available,
available,28800,1536,14400,768
available,28800,1536,14400,768available,
available,28800,1536,14400,768
available,28800,1536,14400,768available,
available,28800,1536,14400,768
available,28800,1536,14400,768available,
available,28800,1536,14400,768
available,28800,1536,14400,768
目标是得到这个
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
知道如何解决这个问题吗?
谢谢
如果您将 grep 更改为一行,它会起作用(删除 for 循环):
dte="available"
files=$(cat rimco)
out=`grep 768 $files`
output=`echo -e "$out" | sed "s/^.*:/$dte,/"`
echo -e "$output"
sed 现在用 $dte 字符串替换文件名(grep 的结果)加上冒号 :
这是我对许多要 grep 的文件的回答。使用额外的变量 result 进行连接,并使用 grep 删除空行:
dte="available"
result=""
for f in $(cat rimco)
do
out=`grep 768 $f`
output=`echo -e "$out" | sed "s/^/$dte,/"`
result="$result\n$output"
done
echo -e "$result" | grep -v "^$"
这个returns:
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
我试图远离所有 运行 时间临时文件。我能够用 运行 时间变量替换所有这些,但是当我将命令与变量一起使用时,一个命令给我带来了困难。
rimco 内容:包含文件位置
/home/bim
/home/ram
/home/gps
/home/hdr
/home/pal
bim内容:包含一组数字
28800,1536,14400,768
11100,12312,902,321
3044,1536,1290,334
3044,1536,1290,334
11100,12312,902,321
28800,1536,14400,768
目标是在找到我要查找的号码时为变量添加前缀 $output
。该行存储在 $output
中,前缀为 $dte
dte="available"
for f in $(cat rimco)
do
out=`grep 768 $f`
output+=`echo -e "\n$out"| sed "s/^/$dte,/"`
done
嗯,结果不好看,dte内容,到处都是
available,
available,28800,1536,14400,768
available,28800,1536,14400,768available,
available,28800,1536,14400,768
available,28800,1536,14400,768available,
available,28800,1536,14400,768
available,28800,1536,14400,768available,
available,28800,1536,14400,768
available,28800,1536,14400,768available,
available,28800,1536,14400,768
available,28800,1536,14400,768
目标是得到这个
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
知道如何解决这个问题吗?
谢谢
如果您将 grep 更改为一行,它会起作用(删除 for 循环):
dte="available"
files=$(cat rimco)
out=`grep 768 $files`
output=`echo -e "$out" | sed "s/^.*:/$dte,/"`
echo -e "$output"
sed 现在用 $dte 字符串替换文件名(grep 的结果)加上冒号 :
这是我对许多要 grep 的文件的回答。使用额外的变量 result 进行连接,并使用 grep 删除空行:
dte="available"
result=""
for f in $(cat rimco)
do
out=`grep 768 $f`
output=`echo -e "$out" | sed "s/^/$dte,/"`
result="$result\n$output"
done
echo -e "$result" | grep -v "^$"
这个returns:
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768
available,28800,1536,14400,768