Removing lines within a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing lines within a file
# 1  
Old 08-18-2006
Removing lines within a file

Hi There,

I've written a script that processes a data file on our system. Basically the script reads a post code from a list file, looks in the data file for the first occurrence (using grep) and reads the line number. It then tails the data file, with the line number just read, and outputs to a temp file. Then greps the temp file for the last occurrence of the post code and reads the line number to a variable, this then goes to another temp file. The script then greps for the last line break (^L) and reads the line number. Now this may sound complicated, if not already, but it works... the script works out the number of lines between the last post code occurrence and the last line break and adds these together to get a final line number. The script outputs the data within the first and last line numbers to a new file and this gets formatted all nicely and sent to a client. The script runs in a loop for 43 different post codes until a variable reads 'finish'. There is data within the data file that is not processed (ie unneeded) what I need to do is put a few lines in the script that removes either, the data that will be emailed or the unprocessed data.

I've had a look at SED & AWK but cannot find anything suitable at the minute.
EDIT: My goal is to have a new output file with the unneeded data. The format of the data in the file is the same... I just don't know what data i'm looking for. It should be simple enough though, in theory, to have all data that i want emailed removed from the original data file and the other data in a seperate file. I may have rambled on some, but if you require any code snippets just drop me a message.


Any suggestions?

thanks

Last edited by tookers; 08-18-2006 at 12:21 PM..
# 2  
Old 08-18-2006
If you actually have line numbers sed will let you print all the not needed data using line numbers. Assume lines 10-20 and 24-28 and 35-45 are good (this goes on for a total of 43 of line blocks) leaving the rest as not needed.
Code:
sed /10,20d;24,28d;35,45d/ filename > useless.data

try a script to generate the sed statement as a one-line shell script:
Code:
#!/bin/ksh
set -A start 10 20 30 40 50 60 70 80 90 100
set -A stop  15 25 35 45 55 67 75 85 95 105
filename="InCSFBils_2006-08-18-06.20.DAT"
newfilename="testfile"

printf "sed \'" > dump.sh
let i=0
while [[ $i -lt 10 ]]
do
    printf "%d,%dd;" ${start[i]} ${stop[i]} >> dump.sh
    let i=$i+1
done
printf "\' $filename > $newfilename\n" >> dump.sh
chmod +x dump.sh
dump.sh

dump.sh looks like this:
Code:
sed '10,15d;20,25d;30,35d;40,45d;50,55d;60,67d;70,75d;80,85d;90,95d;100,105d;' InCSFBils_2006-08-18-06.20.DAT > testfile

You will have to dynamically add elements to your arrays.
# 3  
Old 08-18-2006
That is exactly what I needed, thanks for that. I've created a test script on my machine at home, will test it on the production script next week at work.

Thanks.

EDIT: Heres what I've got, seems to work fine on HPUX.

Code:
FIRSTLINE=6
END=12
FILE=/prg/scripts/RoomTesting/in/testin.txt
rm /prg/scripts/RoomTesting/garbage.sh
printf "sed -e '" > /prg/scripts/RoomTesting/garbage.sh
printf "$FIRSTLINE,$END" >> /prg/scripts/RoomTesting/garbage.sh
printf "d' $FILE > /prg/scripts/RoomTesting/in/output.txt\n" >> /prg/scripts/RoomTesting/garbage.sh
chmod 755 /prg/scripts/RoomTesting/garbage.sh
/prg/scripts/RoomTesting/garbage.sh

OUTPUT FROM SCRIPT:
this is line 1
this
info
is
needed
this
info
is
needed
this is line 17

All other info is left as is in the original file.

Last edited by tookers; 08-21-2006 at 11:21 AM..
# 4  
Old 08-22-2006
Hi again,

I've got another problem now.
My script now creates a sed script with a range of first & end lines.
Heres what the sed script looks like
Code:
sed -e '143,221d;87,144d;411,491d;1,88d;299,412d;220,300d;' /prg/scripts/RoomTes
ting/in/220806.lst > /prg/scripts/RoomTesting/in/output.txt

It appears to run fine, however, on checking the output file (output.txt) it seems to completely ignore the final set of line numbers (220,300).
If I put these line numbers as the first range they get removed however the last range always seems to get ignored.

Any help?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing lines from a file

I have a file `/tmp/wrk` containing filenames with paths. I want to remove filenames from this file, for example remove all filenames containing alja cagr cavt clta cmdo or corl remove all filenames containing data for days in region `d.2016.001` to `d.2016.207` remove all filenames... (10 Replies)
Discussion started by: kristinu
10 Replies

2. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

3. Shell Programming and Scripting

Removing lines from a file

Hi, I have a linux server that was hacked and I have a bunch of files that sporadically contain the following lines through out the file: <?php eval(base64_decode("Xxxxxxxxxxxxxx/xxxxxxxx")); I did't put the exact lines of the file in this post. The "Xxxx" are random letters/numbers.... (8 Replies)
Discussion started by: nck
8 Replies

4. UNIX for Dummies Questions & Answers

removing several lines from a file

Hi folks, I have a long string of DNA sequences, and I need to remove several lines, as well as the line directly following them. For example, here is a sample of my starting material: >548::GY31UMJ02DLYEH rank=0007170 x=1363.5 y=471.0 length=478... (1 Reply)
Discussion started by: kkohl78
1 Replies

5. Shell Programming and Scripting

removing lines from file

Hi I have many files all with 1 field per line as in 12345 abcde john.paul.net 6789101 how do I remove ceratin lines from these files. Have tried sed but sed wrecks my head! Many thanks in advance for any help (9 Replies)
Discussion started by: rob171171
9 Replies

6. Shell Programming and Scripting

Removing Lines From a File

Hi Does anybody know of a command that will enable me to remove all entries in a file that have the format (name & time) more testfile anthony 2003 anthonyr 2008 amorel 15:00 anthonyp 14:35 anthonyp 14:35 anthonyr 2008 ardean 13:28 arlene 2003 arlenem 08:15 arlenem 08:15... (5 Replies)
Discussion started by: jamba1
5 Replies

7. Shell Programming and Scripting

Removing the first and last lines in a file

Hi Gurus, I'm a little new to UNIX. How can I do remove the first and last line in a file? Say, supppose I have a file as below: Code: 1DMA 400002BARRIE 401002CALGARY/LETHBRI 402002CARLETON 500001PORTLAND-AUBRN 501001NEW YORK, NY 502001BINGHAMTON, NY ... (2 Replies)
Discussion started by: naveendronavall
2 Replies

8. AIX

Removing the first and last lines in a file

Hi Gurus, I'm a little new to UNIX. How can I do remove the first and last line in a file? Say, supppose I have a file as below: 1DMA 400002BARRIE 401002CALGARY/LETHBRI 402002CARLETON 500001PORTLAND-AUBRN 501001NEW YORK, NY ... (1 Reply)
Discussion started by: naveendronavall
1 Replies

9. UNIX for Dummies Questions & Answers

Removing lines from a file

I'm trying to find a command which will allow me to remove a range of lines (2-4) from a .dat file from the command line without opening the file. Someone mentioned using the ex command? Does anyone have any ideas? thanks (6 Replies)
Discussion started by: computersaysno
6 Replies

10. Shell Programming and Scripting

Removing lines from a file

Hello i have 2 files file1 and file2 as shown below file1 110010000000206|567810008161509 110010000000207|567810072227627 110010000000208|567811368851555 110010000000209|567811422513652 110010000000210|567812130217683 110010000000211|567813220211182 110010000000212|567813449322589... (4 Replies)
Discussion started by: PradeepRed
4 Replies
Login or Register to Ask a Question