How to delete a row in a file?

Does anyone know a way to delete rows 6-9 from the below output? I have searched the forum but did not find any thing helpful.

backups01laxint.liuc(s){jsandova}[~]<0>$ nsrjb -v | awk -F' ' '$3>=01{print $0}' | cut -b 1-79 | cat -n
setting verbosity level to `1'
1 slot volume used pool barcode
2 1: intwindows.0005 9% intwindows 010159L2
3 2: intunix.0009 19% intunix 010155L2
4 4: intrman.0002 2% intrman 010395L2
5 24: Cleaning Tape (50 uses left) 000002L1 000002L1
6 *not registered in the NetWorker media data base
7 7 registered volume(s), 7 volumes are less than 80% full.
8 1335 GB estimated capacity, 1278 GB remaining (4% full)
9 Default slot range(s) are 1-23

If you mean remove the 6th to 9th line, try this:

#!/usr/bin/perl
# You may need to change this path to /usr/local/bin/perl

$linecount = 0;

open (FILE,"input.txt");
@lines = <FILE>;
close (FILE);

open (FILE,">input.txt");
foreach $l (@lines) {
 $linecount++;
 if ($linecount ne 6 && $linecount ne 7 && $linecount ne 8 && $linecount ne 9) {
  print FILE $l;
 }
}
close (FILE);

exit;

If you mean the lines numbered 6 to 9, try:

#!/usr/bin/perl
# You may need to change this path to /usr/local/bin/perl

open (FILE,"input.txt");
@lines = <FILE>;
close (FILE);

open (FILE,">input.txt");
foreach $l (@lines) {
 if ($l !~ /^6/ && $l !~ /^7/ && $l !~ /^8/ && $l !~ /^9/) {
  print FILE $l;
 }
}
close (FILE);

exit;
grep -v "^[6-9] " file

if you have PHP

<?php
$file = "file";
$lines = file($file);
foreach ( $lines as $k=>$v){
 $k=(int)$k;;
 if ( ( $k < 6 ) || ( $k > 9 )){
    echo "$v";
 }
}
?>

ghostdog74,
I tried that grep -v example however it didn't work. Is there a way to delete rows with awk or sed?

backups01laxint.liuc(s){jsandova}[~]<0>$ nsrjb -v | awk -F' ' '$3>=01{print $0}' | cut -b 1-79 | cat -n | grep -v "^[6-9] "
setting verbosity level to `1'
1 slot volume used pool barcode
2 1: intwindows.0005 9% intwindows 010159L2
3 2: intunix.0009 19% intunix 010155L2
4 4: intrman.0002 2% intrman 010395L2
5 24: Cleaning Tape (50 uses left) 000002L1 000002L1
6 *not registered in the NetWorker media data base
7 7 registered volume(s), 7 volumes are less than 80% full.
8 1335 GB estimated capacity, 1278 GB remaining (4% full)
9 Default slot range(s) are 1-23

it works for me

# grep -v "^[6-9] " file
backups01laxint.liuc(s){jsandova}[~]<0>$ nsrjb -v | awk -F' ' '$3>=01{print $0}' | cut -b 1-79 | cat -n
setting verbosity level to `1'
1 slot volume used pool barcode
2 1: intwindows.0005 9% intwindows 010159L2
3 2: intunix.0009 19% intunix 010155L2
4 4: intrman.0002 2% intrman 010395L2
5 24: Cleaning Tape (50 uses left) 000002L1 000002L1

It's not working for the original poster because the lines do not start with the numbers.

Here is what he's actually starting with.

backups01laxint.liuc(s){jsandova}[~]<0>$ nsrjb -v |  awk -F' ' '$3>=01{print $0}'  | cut -b 1-79 | cat -n | grep -v "^[6-9] "
setting verbosity level to `1'
     1    slot  volume                                     used  pool        barcode
     2       1: intwindows.0005                              9%  intwindows  010159L2
     3       2: intunix.0009                                19%  intunix     010155L2
     4       4: intrman.0002                                 2%  intrman     010395L2
     5      24: Cleaning Tape (50 uses left) 000002L1                            000002L1
     6          *not registered in the NetWorker media data base
     7        7 registered volume(s), 7 volumes are less than 80% full.
     8     1335 GB estimated capacity, 1278 GB remaining (4% full)
     9          Default slot range(s) are 1-23

Even my 2nd Perl version won't work. Need this:

#!/usr/bin/perl
# You may need to change this path to /usr/local/bin/perl

open (FILE,"input.txt");
@lines = <FILE>;
close (FILE);

open (FILE,">input.txt");
foreach $l (@lines) {
 if ($l !~ /^(\s|\t)*[6-9]/) {
  print FILE $l;
 }
}
close (FILE);

exit;

Here is ghostdog74's version adjusted:

grep -v "^(\s|\t)*[6-9] " file

Possibly there is are spaces at the beginning

Try this sample.

sed 's/^  *//g' b | grep -v "^[6-9] "

( certainly, not an efficient way of doing it )

Thanks for the input PWSwebmaster however that grep-v you adjusted still didn't work. The closest I can get is doing the following but it's still doesn't delete all the lines I want.

backups01laxint.liuc(s){jsandova}[~]<0>$ nsrjb -v | awk -F' ' '$3>=01{print $0}' | cut -b 1-79 | grep -v "("
setting verbosity level to `1'
slot volume used pool barcode
1: intwindows.0005 9% intwindows 010159L2
2: intunix.0009 19% intunix 010155L2
4: intrman.0002 2% intrman 010395L2
*not registered in the NetWorker media data base

Making slight modifications

$l =~ s/^  *//g;
if (!( $l >= 6 && $l <= 9 )) {
  print FILE $l;
}

Try escaping the round brackets.

grep -v "^\(\s|\t\)*[6-9] " file

That'll work too, but don't need the global option since anchored matches can only match once. :wink:

 # grep  -v "^ *[6-9]" file
backups01laxint.liuc(s){jsandova}[~]<0>$ nsrjb -v |  awk -F' ' '$3>=01{print $0}'  | cut -b 1-79 | cat -n | grep -v "^[6-9] "
setting verbosity level to `1'
     1    slot  volume                                     used  pool        barcode
     2       1: intwindows.0005                              9%  intwindows  010159L2
     3       2: intunix.0009                                19%  intunix     010155L2
     4       4: intrman.0002                                 2%  intrman     010395L2
     5      24: Cleaning Tape (50 uses left) 000002L1            

You are right !

$l =~ s/^  *//;
print FILE $1 if (!( $l >= 6 && $l <= 9 ));

What about this solution in general:

..... | sed 6,9d 
..... | awk 'NR~"[6-9]"{next}1'

or for the OP request in special:

.... | sed 5q
.... | awk 'NR==6{exit}1'

... and don't make your life complicated :slight_smile:

danmero,
That is exactly what I was look for. Simple and short, I however have not used sed much so was not sure on how to implement it. Thanks for the great tip!

backups01laxint.liuc(s){jsandova}[~]<0>$ nsrjb -v | awk -F' ' '$3>=01{print $0}' | cut -b 1-79 | sed 5q
setting verbosity level to `1'
slot volume used pool barcode
1: intwindows.0005 9% intwindows 010159L2
2: intunix.0009 20% intunix 010155L2
4: intrman.0002 2% intrman 010395L2
24: Cleaning Tape (50 uses left) 000002L1 000002L1

Oops,
it should be:

awk '!(NR~/^[6-9]$/)'

or:

awk 'NR~/^[6-9]$/{next}1'

I like this one :slight_smile: