Replacing Awk with One-liner Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing Awk with One-liner Perl
# 1  
Old 12-23-2011
Replacing Awk with One-liner Perl

can someone help me translate the following command, from:

Code:
/usr/bin/awk "/^$TOFDAYM  $TOFDAYD /,0" $LOGFILE

to something like

Code:
perl -e .....

basically, i want to use perl to do awk functions within a shell script. i want to do the above awk, using perl.

any suggestions?
# 2  
Old 12-23-2011
Code:
 
perl -lane 'BEGIN{$f=0} $a=$_;if ($a=~/^$TOFDAYM  $TOFDAYD/ && ($f eq 0)){print $a; $f=$f+1;}elsif ($f gt 0){print $a;}' $LOGFILE

---------- Post updated at 10:27 AM ---------- Previous update was at 10:22 AM ----------

Code:
perl -lane 'BEGIN{$f=0} if ($_=~/^$TOFDAYM  $TOFDAYD/ && $f eq 0){print $_; $f++}elsif ($f gt 0){print $_}' $LOGFILE

# 3  
Old 12-23-2011
1. I don't think your awk one-liner would work as you're not passing $TOFDAYM and $TOFDAYD from shell to awk.
2. When you say 0 at the end, it would print the line regardless of whether the condition is met or not.

What is your requirement exactly? Please provide a sample input and sample output. It would help us to build a perl one-liner.
# 4  
Old 12-23-2011
Quote:
Originally Posted by balajesuri
1. I don't think your awk one-liner would work as you're not passing $TOFDAYM and $TOFDAYD from shell to awk.
2. When you say 0 at the end, it would print the line regardless of whether the condition is met or not.

What is your requirement exactly? Please provide a sample input and sample output. It would help us to build a perl one-liner.

thank you for your response.

this is to be used on a log file. i.e., the /var/log/messages file.

what i want to do is search for a date, like "Dec 23". such a date would of course be the first entry in the lines of the log. hence the need for the "^".

but here's what i need to do. i need to search for a date in a log file, and when that date is found, i need to spit out to screen every line/record that comes after that date including the first matching line.

the above perl one liner provided by itkamaraj didn't seem to work. it just hung there and didn't do anything. anymore suggestions?

thanks a lot guys
# 5  
Old 12-23-2011
Are you looking for something like this?

Code:
user@ubuntu:~/programs$ cat input
This is the first line of log file
Dec 23
This is the third line
This is the fourth line

Code:
user@ubuntu:~/programs$ perl -ne '(/^Dec 23/)&&($p=1);($p==1)&&(print)' input
Dec 23
This is the third line
This is the fourth line

# 6  
Old 12-23-2011
Quote:
Originally Posted by balajesuri
Are you looking for something like this?

Code:
user@ubuntu:~/programs$ cat input
This is the first line of log file
Dec 23
This is the third line
This is the fourth line

Code:
user@ubuntu:~/programs$ perl -ne '(/^Dec 23/)&&($p=1);($p==1)&&(print)' input
Dec 23
This is the third line
This is the fourth line

The Log looks likes something like this:

Code:
Dec 23 20:54:84 This is the first line of the log.  This is great. I am so happy.
Dec 23 20:54:14 This is the sec line of the log.  This is great. I am so happy.
Dec 23 20:54:14 This is the thir line of the log.  This is great. I am so happy.
Dec 23 20:54:14 This is the fou line of the log.  This is great. I am so happy.
Dec 23 20:54:14 This is the fifth line of the log.  This is great. I am so happy.

# 7  
Old 12-23-2011
Ok.. What happened? Isn't the perl one-liner working?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Perl one liner in bash script not replacing hours and minutes [HH:MM]

Hi I want to replace time stamp in the following line PROCNAME.Merge.exchMon.CODE.T_QSTART 08:45 read assuming the new time stamp is 09:45 ; the line is getting replaced as below :45 read I'm trying to use the perl one liner in bash script perl -pi... (4 Replies)
Discussion started by: charlie87
4 Replies

2. Shell Programming and Scripting

Checking and replacing first line in text file, one-liner?

Hello, I'm looking to check only the first line of a file to see if it is a format string, like # -*- coding: utf-8; mode: tcl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -\*- vim:fenc=utf-8:ft=tcl:et:sw=2:ts=2:sts=2if the first line is anything else, insert the above string. I'd... (3 Replies)
Discussion started by: f77hack
3 Replies

3. Shell Programming and Scripting

PERL one liner

hi, I am using PERL one liner for oracle database connection as : $PERL -e "use DBI; DBI->connect(qw(DBI:Oracle:SID user passwd));" is there a way to append select statement to this connection ? i.e. DB connection and select stmt in one line ? how to do sysdba connection using one lines... (1 Reply)
Discussion started by: talashil
1 Replies

4. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

5. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

replace awk with a perl one liner (REGEXP and FS)

hello, I want to replace awk with a perl one liner in unix. i use in awk REGEX and FS ( field separator) because awk syntaxes in different unix os versions have not the same behaviour. Awk, Nawk and GNU Awk Cheat Sheet - good coders code, great reuse i have a file named "file" and want... (5 Replies)
Discussion started by: bora99
5 Replies

7. Shell Programming and Scripting

Need an awk / sed / or perl one-liner to remove last 4 characters with non-unique pattern.

Hi, I'm writing a ksh script and trying to use an awk / sed / or perl one-liner to remove the last 4 characters of a line in a file if it begins with a period. Here is the contents of the file... the column in which I want to remove the last 4 characters is the last column. ($6 in awk). I've... (10 Replies)
Discussion started by: right_coaster
10 Replies

8. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

9. Shell Programming and Scripting

awk/perl one-liner assist

In a ~4GB file there are lines like, 13.13.4.3 Googe.com - Jan/23/2011:00:00:00 +0000 "URL Google HTTP/1.1" 45 56 208 - "http://www.gogle.com/webhp?hl=en&tab=nw#hl=en&source=hp&biw=1366&bih=667&q=hello&aq=f&aqi=&aql=&oq=&fp=c432485467934a89" ".Net; Fox" - 13.145.3.3 Goge.com -... (3 Replies)
Discussion started by: gameboy87
3 Replies

10. Shell Programming and Scripting

Perl One Liner

Hi , Can anybody explain how this perl one liner works.. It is to test whether the number is prime or not perl -le 'print "PRIME" if (1 x shift) !~ /^(11+)\1+$/' 19 Thanks in advance Shihab (2 Replies)
Discussion started by: shihabvk
2 Replies
Login or Register to Ask a Question