Re: Deleting lines from a script. (72 Views)
Reply
Advisor
Reynaldo Torres
Posts: 47
Registered: ‎04-13-1998
Message 1 of 4 (72 Views)
Accepted 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
Please use plain text.
Outstanding Contributor
Pete Randall
Posts: 16,205
Registered: ‎11-03-1996
Message 2 of 4 (72 Views)

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
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 4 (72 Views)

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...
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 4 (72 Views)

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...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation