Copying the Header & footer Information to the Outfile.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying the Header & footer Information to the Outfile.
# 8  
Old 08-26-2011
Hi Yazu,

Thanks a lot for your reply.

When I am using the below line:
Code:
$n -= grep {/^# PRODUCT/} @goodlines;

The "#" sign is actually commenting the entore line from there.i.e
Code:
# PRODUCT/} @goodlines;

commenting the above.

Is there any way to check for the above lines ?

Thanks much for your replies.
# 9  
Old 08-26-2011
No. # is an usual char in string literals and regexes :
Code:
cat >INPUTFILE
1
# comment
3
#
#
4
$ perl -lne '!/^#/ && print' INPUTFILE 
1
3
4

What the error did you get?
This User Gave Thanks to yazu For This Post:
# 10  
Old 08-27-2011
I am Sorry Yazu. Its was my mistake.

You are Excellent. Smilie

I have learnt a lot more from your posts. Really appreciated for your knowledge sharing.

Bunch of thanks to you. you rock! Smilie Smilie
# 11  
Old 08-29-2011
Hi Yazu/All,

I have been trying to write the same logic in a different way:
i.e. Instead of loading the file into an array (memory), I would like to read the file line by line and then check for the conditions:
The code looks lile:
Code:
my $goodfile;
my $badfile;
my $footer_len = 4;
my $datarec_line = 1;
my ( @header, @footer, @goodlines, @badlines, @fields);
my $line;
$line = $_;
while (<FILE>)
{
$line = $_;
@fields = split (/\|/, $line);

if( $fields[57] eq " ")
{
 push @badlines, $line;
}

elsif( $fields[32] eq "N.A."  && $fields[33] eq "N.A." && $fields[34] eq "N.A." && $fields[38] eq "N.A." && ($fields[62] eq "N.A." ||  $fields[62] eq " "))
{
push @badlines, $line;
}

else
{
push @goodlines, $line;
}

}

@footer = splice @goodlines, -$footer_len;

my $n = @goodlines;
$n -= grep {/^# PRODUCT/} @goodlines;
$footer[$datarec_line] =~ s/\d+/$n/;

open my $fh, ">", $goodfile;
print $fh  @header, @goodlines, @footer;
close $fh;

open my $fh1, ">", $badfile;
print $fh1 @badlines;
close $fh1;

Here:
The script runs perfectly. It actually read line by line and then check for conditions.
But,
The header contains 107 lines i.e.
Code:
START-OF-FILE .......  so on ....
# PRODUCT=Corp/Pfd   --> 107 Line

Since its 107 lines, while I am counting the number of lines its calculating the entire number of lines including the header and then doing the grep and substituting but instead it should exclude the header and do the remaining.

Other than that The footer looks good.

Could you please tell me whether is there any way to that after inserting the good lines into an array, calculating the number of lines without the header ?

Really appreciate your replies.
# 12  
Old 12-20-2011
Hi Yazu / All,

I am trying to implement and extend the logic more further.

The only issue is that the memory is reporting as full if a huge file is being passed to the script.

Currently the script is using the below logic:
Code:
while (<FILE>) { $line = $_; @fields = split (/\|/, $line);

Is there anyway we can use a hash or any other approach ?

Thanks for all your time and thoughts!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Strip header and footer

Hi I have below requirements on the script below : (1) I receive 2 pipe seperated file called OUT.psv and DIFF.psv with a column header.I concatenate the 2 files and create a final.psv file. I want to add another header as START_FILE to the final.psv file . How to achieve this ? (2) I have... (5 Replies)
Discussion started by: samrat dutta
5 Replies

2. Shell Programming and Scripting

Add header and footer with record count in footer

This is my file(Target.txt) name|age|locaction abc|23|del xyz|24|mum jkl|25|kol The file should be like this 1|03252012 1|name|age|location 2|abc|23|del 2|xyz|24|mum 2|jkl|25|kol 2|kkk|26|hyd 3|4 Column 1 is row indicator for row 1 and 2, column indicator is 1,for data rows... (1 Reply)
Discussion started by: itsranjan
1 Replies

3. Shell Programming and Scripting

Removing header and footer

I have two files which are getting sent to a UNIX server in order to be bcp'd into a database. The bcp is failing because there's a header and footer row on the file which give the date of the file and the number of rows in it. That's because the file is also being used for another process, so we... (1 Reply)
Discussion started by: Tom Sawyer
1 Replies

4. Shell Programming and Scripting

Header and Footer...

Hi All, I need to write a script that In my file I have to check header and footer records are available or not. If it is available I have to run the script, otherwise I should not. But current script it is checking only the data inside the script. It is avoiding to check Header and Footer... (1 Reply)
Discussion started by: suresh_target
1 Replies

5. Shell Programming and Scripting

copying file information using awk & grep

Hi, TASK 1: I have been using this code to print the information of files kept at "/castor/cern.ch/user/s/sudha/forPooja" in some text file name FILE.txt. rfdir /castor/cern.ch/user/s/sudha/forPooja | grep data | awk '{print "rfio:///castor/cern.ch/user/s/sudha/forPooja/"$9}' > FILE.txt ... (6 Replies)
Discussion started by: nrjrasaxena
6 Replies

6. UNIX for Dummies Questions & Answers

Help with the Header and Footer check

Hi, I need to check whether the incoming file has a header and footer using a UNIX script. The pattern of the header and footer is fixed as follows: Header: Name,Date Footer: Count, Total Name,Date ------------------------- ------------------------- ------------------------- Count,... (5 Replies)
Discussion started by: Sunny_teotia
5 Replies

7. Shell Programming and Scripting

How to add header and footer?

Hi, Guys, I want add header and footer in a file. I can add footer using following command: echo "Footer" >>file. I don't know how to add header. Thanks in advance (4 Replies)
Discussion started by: ken002
4 Replies

8. Shell Programming and Scripting

Inserting Header and footer

Hi All, I have several txt files i need to enter specific header and footer (both are separate) to all these files how can i do this? plz help.. Regards, Raghav (4 Replies)
Discussion started by: digitalrg
4 Replies

9. Shell Programming and Scripting

rowcnt except Header & Footer

Hi Gurus, My requirement is, I am passing a file1.dat into this(rowcnt.sh) script,but returning a wrong value of -2.(it should be 4).Becoz my file1.dat contains 6records incl: Header & Footer.(6-2=4) wrong output: ------- #sh rowcnt.sh file1.dat -2 actual_cnt except HDR & FTR should be:... (3 Replies)
Discussion started by: vsubbu1000
3 Replies

10. Shell Programming and Scripting

remove header and footer rows

I would like to remove some lines from begining of file (header) and some lines from end of file (footer). The header/footer lines generated by web-browser when the user upload a file to my webserver. Example: -----------------------------7d62af20c052c Content-Disposition: form-data;... (2 Replies)
Discussion started by: seaky
2 Replies
Login or Register to Ask a Question