【问题标题】:How to delete the set of range lines and last line using bash scripting?如何使用 bash 脚本删除范围行和最后一行?
【发布时间】:2019-09-11 11:10:36
【问题描述】:

如何从日志文件中删除这组几行和最后一行?

我有一个日志文件,其中包含以下行集:-

查询:

select schema_name from information_schema.schemata where schema_name ='emp_his';
 schema_name
-------------
 emp_his
(1 row)

我只想要 emp_his 字符串其余所有行被删除。 我已经准备好它正在工作的脚本头和尾命令。我已经粘贴了两个代码,但是$d 命令不起作用,为什么?
此代码有效:

databasename="$(cat /home/enterprisedb/.bash_profile |grep PGDATABASE|awk '{print $1}'|cut -d '=' -f2|cut -d ';' -f1)"
echo "$databasename"


listof_schema="select schema_name from information_schema.schemata where schema_name ='emp_his';"


var2="$(psql -U enterprisedb -d $databasename -c "$listof_schema" -L /home/enterprisedb/schema_name.log)"

sed -i -e "1,6d" /home/enterprisedb/schema_name.log
final_schema_list="$(cat /home/enterprisedb/schema_name.log| head -1|tr -d "[:blank:]" > /home/enterprisedb/final_list.log)"
count="$(cat /home/enterprisedb/final_list.log)"
echo "$count"

但下面的代码不起作用:

databasename="$(cat /home/enterprisedb/.bash_profile |grep PGDATABASE|awk '{print $1}'|cut -d '=' -f2|cut -d ';' -f1)"
echo "$databasename"


listof_schema="select schema_name from information_schema.schemata where schema_name ='emp_his';"


var2="$(psql -U enterprisedb -d $databasename -c "$listof_schema" -L /home/enterprisedb/schema_name.log)"

sed -i -e "1,6d" /home/enterprisedb/schema_name.log
sed -i -e "$d" /home/enterprisedb/schema_name.log > /home/enterprisedb/final_list.log

count="$(cat /home/enterprisedb/final_list.log)"
echo "$count"

我希望这段代码能够正常工作:

databasename="$(cat /home/enterprisedb/.bash_profile |grep PGDATABASE|awk '{print $1}'|cut -d '=' -f2|cut -d ';' -f1)"
echo "$databasename"


listof_schema="select schema_name from information_schema.schemata where schema_name ='emp_his';"


var2="$(psql -U enterprisedb -d $databasename -c "$listof_schema" -L /home/enterprisedb/schema_name.log)"

sed -i -e "1,6d" /home/enterprisedb/schema_name.log
sed -i -e '$ d' /home/enterprisedb/schema_name.log > /home/enterprisedb/final_list.log

count="$(cat /home/enterprisedb/final_list.log)"
echo "$count"

我只想要日志文件中的emp_his 字符串,其余的都应该删除。 这可以通过 head 和 tail 命令来实现,但是这个命令 sed -i -e "$d" /home/enterprisedb/schema_name.log 在 Bash 脚本中不起作用。

【问题讨论】:

标签: linux bash


【解决方案1】:

$d sed 工作正常,但没有按照您的想法执行。因为您已经为 sed 提供了 -i 选项,所以它会更新文件而不是打印到标准输出。

试试

sed -e "1,6d" -e '$d' /home/enterprisedb/schema_name.log >/home/enterprisedb/final_list.log

【讨论】:

  • 感谢您的建议。我试过你的代码还是不行。当我运行代码"var2="$(psql -U enterprisedb -d $databasename -c "$listof_schema" -L /home/enterprisedb/schema_name.log)"" 我得到以下输出:- *** ****** 查询 ********** 从 information_schema.schemata 中选择 schema_name 其中 schema_name ='emp_his'; ****************************** schema_name ------------- emp_his(1 行)
  • 但是当我运行 "sed -e "1,6d" -e '$d' /home/enterprisedb/schema_name.log >/home/enterprisedb/final_list.log" 输出是:- emp_his (1 行)但我只想要 emp_his 不是(1 行)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-31
  • 2020-06-25
  • 2017-07-09
  • 2011-01-24
  • 2017-10-28
  • 2010-09-25
  • 2013-12-19
相关资源
最近更新 更多