Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Deleting lines from a script.

SOLVED
Go to Solution

Deleting lines from a script.

If I want to delete the first and the last line of text how could I do it. I would really appreciated. Your help is greatly appreciated. Below is the first line that I would like to delete that gets generated from the script listed here rebuild_inx.sh, and creates this file rebuild_inx.sql. So, once this file rebuild_inx.sql is created I would like to eliminate the text lines that gets generated as the regular script. I hope you understand what I am trying to say.

SQL> select ' ALTER INDEX '|| index_name || ' REBUILD PARALLEL 4 NOLOGGING ONLIN
E ;' FROM USER_INDEXES WHERE TABLE_OWNER LIKE 'SYSADM';


Here is the regular script that runs. Please check below.

rebuild_inx.sh

#!/usr/bin/sh
# Rebuild All Indexes
export ORACLE_SID=CRM89DMO
sqlplus sysadm/desk911@CRM89DMO <set echo off pagesize 0 feedback off verity off heading off
spool rebuild_inx.sql
select ' ALTER INDEX '|| index_name || ' REBUILD PARALLEL 4 NOLOGGING ONLINE ;'
FROM USER_INDEXES WHERE TABLE_OWNER LIKE 'SYSADM';
spool off;
exit
EOF1
Reynaldo Torres
3 REPLIES
Highlighted
Outstanding Contributor Outstanding Contributor

Re: Deleting lines from a script.

From "Handy One-Liners for Sed" (attached):

# print all but first line of file
sed 1d infile >> outfile

# print last line of file (emulates "tail -1")
sed '$!d'


Pete

Pete
Acclaimed Contributor Acclaimed Contributor

Re: Deleting lines from a script.

Hi Reynaldo:

A general solution to deleting the first and the last line of a file is this:

# perl -ne 'next if $.==1;next if eof;print' file

If you wish to perform an inplace update, do:

# perl -ni -e 'next if $.==1;next if eof;print' file

To preserve a backup copy of the file before modification, as "*.old" do:

# perl -ni.old -e ...

Regards!

...JRF...
Acclaimed Contributor Acclaimed Contributor

Re: Deleting lines from a script.

Hi (again):

The 'sed' solution can be combined into one-pass:

# sed -n '1d;$d;p' file

The Perl script can be altered slightly (as below) to process any number of files:

# perl -ne 'next if $.==1;close ARGV,next if eof;print' file1 file2 file3 ...

Regards!

...JRF...