Sponsored Content
Top Forums Shell Programming and Scripting File lines starts with # not processed or exclude that lines from processing Post 302906507 by Chenchireddy on Friday 20th of June 2014 07:46:35 AM
Old 06-20-2014
File lines starts with # not processed or exclude that lines from processing

I have a file like below
Code:
#Fields section bald
1234 2345 456 222
abcs dddd dddd ssss
mmmm mmm mmm

i need do not process a files stating with #

I was written code below

Code:
while read -r line
 do
 
          if [ egrep -v '^#'  $file]
          then
         echo ${line} >>
         elif [ $scode -le 399 ]
          then
          echo ${line} >>access.log
          else
         echo ${line}>>error.log
done > $file

it processing but not expectings
and also tried below like
Code:
           while read -r line
 do
 
          if [ egrep -v '^#'  $file]
          then
         echo ${line} >>
         elif [ $scode -le 399 ]
          then
          echo ${line} >>access.log
          else
         echo ${line}>>error.log
done > `egrep -v '^#'  $file`

it is processing but not going to specific file which i am expecting to access and error.logs

Please correct me where should i correct my script.

both are not working as expected

Last edited by Scrutinizer; 06-20-2014 at 08:51 AM.. Reason: CODE Tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting processed lines

I have a log file that I am processing. This contains messages from and to a server (requests and responses). The responses to requests may not be in order i.e. we can have a response to a request after several requests are sent, and in some error cases there may not be any response message. ... (2 Replies)
Discussion started by: BootComp
2 Replies

2. Shell Programming and Scripting

exclude lines in a loop

I use while do - done loop in my shell script. It is working as per my expectations. But I do not want to process all the lines. I am finding it difficult to exclude certain lines. 1) I do not want to process blank lines as well as lines those start with a space " " 2) I do not want to... (2 Replies)
Discussion started by: shantanuo
2 Replies

3. Shell Programming and Scripting

Delete lines that starts with a certain letter

How can I delete those lines that starts with a certain letter? abc def ghi xyz abc def ace gik moq abe imq gxm I want to delete the line that starts with "x". Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

4. UNIX for Dummies Questions & Answers

Display lines not starts with #

hiiiii $ grep ^"#" $file Will give the lines , which starts with # .And I wanna get the lines which are not starting with #. How to implement that. Thanking you Krish:b: (10 Replies)
Discussion started by: krishnampkkm
10 Replies

5. Shell Programming and Scripting

how to delete lines from a file which starts with a specific pattern

I need to delete those lines from a file, which starts with 45. How to do it? (3 Replies)
Discussion started by: mady135
3 Replies

6. Shell Programming and Scripting

How to copy lines that starts with either 3 or 4 into new file?

Hi Guys, I have an awk script that would search the input file for line that starts with a number 3 and copies into a new text file. I want to extend this script to find the lines that either starts with 3 or a or b and copy all those lines into the new file. Here is what I have so far:... (1 Reply)
Discussion started by: Amith821
1 Replies

7. Shell Programming and Scripting

Execution problem ---to remove the lines which starts with one type of character

Hi, I have one file, I need to check if file exist or not and then remove the lines which starts with ? My file1.out data is some thing abcabcppp xyzxyzpqr ????????? ????????? Output should be in test.out abcabcppp xyzxyzpqr I am getting the output as below but the File does not exist... (4 Replies)
Discussion started by: Ramyajiguru1
4 Replies

8. Shell Programming and Scripting

Running sed and counting number of lines processed

/bin/sed -n ';4757335,$ p' | wc -l /bin/sed -n ';4757335,$ p' | egrep "Failed" | egrep -c "PM late arrrival" how can i combine the above two sed commands into one? i want to count the number of lines between the specified line number and the end of the file. AND and i want to count how many... (5 Replies)
Discussion started by: SkySmart
5 Replies

9. Shell Programming and Scripting

File lines starts with # not processed or exclude that lines

I have requirement in my every files starting lines have # needs to be not processing or exclude the that lines. I have written a code like below, but now working as expected getting ERROR" line 60: 1 #!/bin/sh 2 echo ======= LogManageri start ========== 3 4 #This directory is... (1 Reply)
Discussion started by: Chenchireddy
1 Replies

10. Shell Programming and Scripting

Exclude lines in a file with matches with multiple Strings using egrep

Hi I have a txt file and I would like to use egrep without using -v option to exclude the lines which matches with multiple Strings. Let's say I have some text in the txt file. The command should not fetch lines if they have strings something like CAT MAT DAT The command should fetch me... (4 Replies)
Discussion started by: Sathwik
4 Replies
DateTime::Format::Builder::Tutorial(3pm)		User Contributed Perl Documentation		  DateTime::Format::Builder::Tutorial(3pm)

NAME
DateTime::Format::Builder::Tutorial - Quick class on using Builder CREATING A CLASS
As most people who are writing modules know, you start a package with a package declaration and some indication of module version: package DateTime::Format::ICal; our $VERSION = '0.04'; After that, you call Builder with some options. There are only a few (detailed later). Right now, we're only interested in parsers. use DateTime::Format::Builder ( parsers => { ... } ); The parsers option takes a reference to a hash of method names and specifications: parsers => { parse_datetime => ... , parse_datetime_with_timezone => ... , ... } Builder will create methods in your class, each method being a parser that follows the given specifications. It is strongly recommended that one method is called parse_datetime, be it a Builder created method or one of your own. In addition to creating any of the parser methods it also creates a "new()" method that can instantiate (or clone) objects of this class. This behaviour can be modified with the constructor option, but we don't need to know that yet. Each value corresponding to a method name in the parsers list is either a single specification, or a list of specifications. We'll start with the simple case. parse_briefdate => { params => [ qw( year month day ) ], regex => qr/^(dddd)(dd)(dd)$/, }, This will result in a method named parse_briefdate which will take strings in the form 20040716 and return DateTime objects representing that date. A user of the class might write: use DateTime::Format::ICal; my $date = "19790716"; my $dt = DateTime::Format::ICal->parse_briefdate( $date ); print "My birth month is ", $dt->month_name, " "; The "regex" is applied to the input string, and if it matches, then $1, $2, ... are mapped to the params given and handed to "DateTime->new()". Essentially: my $rv = DateTime->new( year => $1, month => $2, day => $3 ); There are more complicated things one can do within a single specification, but we'll cover those later. Often, you'll want a method to be able to take one string, and run it against multiple parser specifications. It would be very irritating if the user had to work out what format the datetime string was in and then which method was most appropriate. So, Builder lets you specify multiple specifications: parse_datetime => [ { params => [ qw( year month day hour minute second ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)(dd)$/, }, { params => [ qw( year month day hour minute ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)$/, }, { params => [ qw( year month day hour ) ], regex => qr/^(dddd)(dd)(dd)T(dd)$/, }, { params => [ qw( year month day ) ], regex => qr/^(dddd)(dd)(dd)$/, }, ], It's an arrayref of specifications. A parser will be created that will try each of these specifications sequentially, in the order you specified. There's a flaw with this though. In this example, we're building a parser for ICal datetimes. One can place a timezone id at the start of an ICal datetime. You might extract such an id with the following code: if ( $date =~ s/^TZID=([^:]+):// ) { $time_zone = $1; } # Z at end means UTC elsif ( $date =~ s/Z$// ) { $time_zone = 'UTC'; } else { $time_zone = 'floating'; } $date would end up without the id, and $time_zone would contain something appropriate to give to DateTime's set_time_zone method, or time_zone argument. But how to get this scrap of code into your parser? You might be tempted to call the parser something else and build a small wrapper. There's no need though because an option is provided for preprocesing dates: parse_datetime => [ [ preprocess => &_parse_tz ], # Only changed line! { params => [ qw( year month day hour minute second ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)(dd)$/, }, { params => [ qw( year month day hour minute ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)$/, }, { params => [ qw( year month day hour ) ], regex => qr/^(dddd)(dd)(dd)T(dd)$/, }, { params => [ qw( year month day ) ], regex => qr/^(dddd)(dd)(dd)$/, }, ], It will necessitate _parse_tz to be written, and that routine looks like this: sub _parse_tz { my %args = @_; my ($date, $p) = @args{qw( input parsed )}; if ( $date =~ s/^TZID=([^:]+):// ) { $p->{time_zone} = $1; } # Z at end means UTC elsif ( $date =~ s/Z$// ) { $p->{time_zone} = 'UTC'; } else { $p->{time_zone} = 'floating'; } return $date; } On input it is given a hash containing two items: the input date and a hashref that will be used in the parsing. The return value from the routine is what the parser specifications will run against, and anything in the parsed hash ($p in the example) will be put in the call to "DateTime->new(...)". So, we now have a happily working ICal parser. It parses the assorted formats, and can also handle timezones. Is there anything else it needs to do? No. But we can make it work more efficiently. At present, the specifications are tested sequentially. However, each one applies to strings of particular lengths. Thus we could be efficient and have the parser only test the given strings against a parser that handles that string length. Again, Builder makes it easy: parse_datetime => [ [ preprocess => &_parse_tz ], { length => 15, # We handle strings of exactly 15 chars params => [ qw( year month day hour minute second ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)(dd)$/, }, { length => 13, # exactly 13 chars... params => [ qw( year month day hour minute ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)$/, }, { length => 11, # 11.. params => [ qw( year month day hour ) ], regex => qr/^(dddd)(dd)(dd)T(dd)$/, }, { length => 8, # yes. params => [ qw( year month day ) ], regex => qr/^(dddd)(dd)(dd)$/, }, ], Now the created parser will create a parser that only runs specifications against appropriate strings. So our complete code looks like: package DateTime::Format::ICal; use strict; our $VERSION = '0.04'; use DateTime::Format::Builder ( parsers => { parse_datetime => [ [ preprocess => &_parse_tz ], { length => 15, params => [ qw( year month day hour minute second ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)(dd)$/, }, { length => 13, params => [ qw( year month day hour minute ) ], regex => qr/^(dddd)(dd)(dd)T(dd)(dd)$/, }, { length => 11, params => [ qw( year month day hour ) ], regex => qr/^(dddd)(dd)(dd)T(dd)$/, }, { length => 8, params => [ qw( year month day ) ], regex => qr/^(dddd)(dd)(dd)$/, }, ], }, ); sub _parse_tz { my %args = @_; my ($date, $p) = @args{qw( input parsed )}; if ( $date =~ s/^TZID=([^:]+):// ) { $p->{time_zone} = $1; } # Z at end means UTC elsif ( $date =~ s/Z$// ) { $p->{time_zone} = 'UTC'; } else { $p->{time_zone} = 'floating'; } return $date; } 1; And that's an ICal parser. The actual DateTime::Format::ICal module also includes formatting methods and parsing for durations, but Builder doesn't support those yet. A drop in replacement (at the time of writing the replacement) can be found in the examples directory of the Builder distribution, along with similar variants of other common modules. SUPPORT
Any errors you see in this document, please log them with CPAN RT system via the web or email: http://perl.dellah.org/rt/dtbuilder bug-datetime-format-builder@rt.cpan.org This makes it much easier for me to track things and thus means your problem is less likely to be neglected. LICENSE AND COPYRIGHT
Copyright X Iain Truskett, 2003. All rights reserved. You can redistribute this document and/or modify it under the same terms as Perl itself. The full text of the licenses can be found in the Artistic and COPYING files included with this document. AUTHOR
Iain Truskett <spoon@cpan.org> SEE ALSO
"datetime@perl.org" mailing list. http://datetime.perl.org/ perl, DateTime, DateTime::Format::Builder perl v5.10.1 2010-03-14 DateTime::Format::Builder::Tutorial(3pm)
All times are GMT -4. The time now is 03:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy