s.kumar
s.kumar

Reputation: 126

How to delete the set of range lines and last line using bash scripting?

How to delete the set of few lines from the log files and the last line?

I have the log file which contains the set of lines like:-

Query:

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

I want only emp_his string rest all lines to be deleted. I have prepared the scripts head and tail command it's working. I have pasted both the code but $d command not working why?
This code is working:

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"

But below code is not working:

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"

I want this code to be functional one:

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"

I want only emp_his string in the log files rest all should be deleted. This can be achieved with the head and tail command but this command sed -i -e "$d" /home/enterprisedb/schema_name.log is not working in the Bash scripting.

Upvotes: 2

Views: 155

Answers (1)

Jon
Jon

Reputation: 3671

The $d sed is working but doesn't do what you think it does. Because you've given sed the -i option, it updates the file in place rather than printing to standard out.

Try

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

Upvotes: 1

Related Questions