How to delete a row in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete a row in a file?
# 1  
Old 10-01-2008
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
# 2  
Old 10-01-2008
If you mean remove the 6th to 9th line, try this:

Code:
#!/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:

Code:
#!/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;

# 3  
Old 10-01-2008
Code:
grep -v "^[6-9] " file

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


Last edited by ghostdog74; 10-01-2008 at 10:40 PM..
# 4  
Old 10-01-2008
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
# 5  
Old 10-01-2008
it works for me
Code:
# 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

# 6  
Old 10-01-2008
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.
Code:
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:

Code:
#!/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;

# 7  
Old 10-01-2008
Here is ghostdog74's version adjusted:
Code:
grep -v "^(\s|\t)*[6-9] " file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to delete row in csv file on date range?

I want to delete row in csv file , which row value from 2009-10-01 to 2011-06-03 using script.my csv row data look like: 2009-10-01 2011-03-30 2011-03-31 2011-04-01 2011-06-03 2011-06-30 2011-07-01 2011-09-28 ... (7 Replies)
Discussion started by: rakibul639
7 Replies

2. Shell Programming and Scripting

Delete duplicate row

Hi all, how can delete duplicate files in file form, e.g. $cat file1 aaa 123 234 345 456 bbb 345 345 657 568 ccc 345 768 897 456 aaa 123 234 345 456 ddd 786 784 234 263 ccc 345 768 897 456 aaa 123 234 345 456 ccc 345 768 897 456 then i need ouput file1 some, (4 Replies)
Discussion started by: aav1307
4 Replies

3. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

4. Shell Programming and Scripting

Parse tab delimited file, check condition and delete row

I am fairly new to programming and trying to resolve this problem. I have the file like this. CHROM POS REF ALT 10_sample.bam 11_sample.bam 12_sample.bam 13_sample.bam 14_sample.bam 15_sample.bam 16_sample.bam tg93 77 T C T T T T T tg93 79 ... (4 Replies)
Discussion started by: empyrean
4 Replies

5. Shell Programming and Scripting

delete a row in csv file based on the date

Hi, I have a csv file with old data..i need to have only last 30 days from the current dateof data in the file.The fourth field in the file is a date field.i need to write a script to delete the old data by comparing the the fourth field with the (current date -30).I need to delete the rows in... (2 Replies)
Discussion started by: pals70423
2 Replies

6. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

7. UNIX for Dummies Questions & Answers

How do you delete cells from a space delimited text file given row and column number?

How do you delete cells from a space delimited text file given row and column number? Letś say the row number is r and the column number is c. Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

8. UNIX for Dummies Questions & Answers

How to delete a row in a file

hi team, i have a file txt1 , in that file there r 10 lines . my aim is to cut 2nd and 6th line of the file . what command we can use for this scenario. (8 Replies)
Discussion started by: natraj005
8 Replies

9. Shell Programming and Scripting

How to Delete Last Row from .csv file in perl

Hi , I've a perl script to convert .xls to .csv .After conversion I want to delete first 28 and the last row from .csv file.Is there any efficent way to achive this both together. I'm deleting first 28 rows by using folllowing my perl code: exec " tail -n+28 infile.csv > outfile.csv ... (7 Replies)
Discussion started by: ajaypatil_am
7 Replies

10. UNIX for Dummies Questions & Answers

Delete first row of csv file

I have a csv file, which is > 2 Gigs. I need to BCP that file to Sybase db , but I cant upload that b'caz first row of the file is failing. ( having some errors probably.) I can manually insert the first line into db & then I can upload the rest of the data in file, if i can delete the first row. ... (2 Replies)
Discussion started by: kedar.mehta
2 Replies
Login or Register to Ask a Question