Is there a way to insert a header at the top of a file? I have a script that scans 5 ip addresses then outputs the 5 ip scan results into one Microsoft excel file in csv format. I want to add a header at the top of the page describing what each column displays such as IP, protocol, port and etc. Currently i have this piece of code to do this:
open (FILE, ">>/root/scanned_ip_addresses.csv"); #open excel file
print FILE "IP,Protocol,Port,State,Service\n"; #Adds header to top of file.
while (<TEXT>) {
...
Unfortunately its not outputting the way I want it to, due to there being five separate scans its outputting a header after each IP scan, instead of just one header. Heres an example of how it outputs:
IP Port Protocol State Service xx xx xxx xxxx xxxxx IP Port Protocol State Service xx xx xxx xxxx xxxxx IP Port Protocol State Service xx xx xxx xxxx xxxxx IP Port Protocol State Service xx xx xxx xxxx xxxxx IP Port Protocol State Service xx xx xxx xxxx xxxxx
I would like it to print out like this:
IP Port Protocol State Service xx xx xxx xxxx xxxxx xx xx xxx xxxx xxxxx xx xx xxx xxxx xxxxx xx xx xxx xxxx xxxxx xx xx xxx xxxx xxxxx
How can i fix this problem?
I get the impression that you’re running your script multiple times and each time it is run, you append new results to your CSV file. If that’s the case, then all you have to do is check the file size right after your
open: