Multi platform script perl or awk

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multi platform script perl or awk
# 1  
Old 10-02-2012
Multi platform script perl or awk

Hi gurus, I am trying to match records in following format:


either awk or perl must be used. I am using cygwin. I wrote following code which works and matches both above entries:
awk 'BEGIN {musr="(-,username,[^)]"} {if ($0~musr) print $0}' netgroup

But if I try to modify this regexp to be more specific the output is nothing:

# 1st: match record then last backslash and then match newline

# 2nd: match new line immediatelly after record without backslash


So i decided to rewrite script into perl, hoping that perl can deal with backslashes and end of line symbols. For this purpose I used a2p this way:

echo  'BEGIN {musr="(-,username,[^)]"} {if ($0~musr) print $0}' | a2p.exe 
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
                        # this emulates #! processing on NIH machines.
                        # (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
                        # process any FOO=bar switches

$, = ' ';               # set output field separator
$\ = "\n";              # set output record separator

$musr = '(-,username,[^)]';

while (<>) {
    chomp;      # strip record separator
    if ($_ =~ $musr) {
        print $_;

This generated perl script also matches both entries, however if I try modify this script to more specific I get the following errors:

$musr = "(-,username,[^)]\\";
Trailing \ in regex m/(-,username,[^)]\/ at line 18, <> line 1.

$musr = "(-,username,[^)]$";
Final $ should be \$ or $name at line 14, within string
syntax error at line 14, near "= "(-,username,[^)]$""
Execution of aborted due to compilation errors.

$musr = "(-,username,[^)]\$";
[the output is nothing]

What I am doing wrong ? My question is also pointing to fact that if somebody needs to use script on several platforms (aix, solaris, linux) than using perl should be better approach that dealing with (non)GNU utils and various (g|n)awk versions etc. Regards
# 2  
Old 10-02-2012
both awk and perl are going to take the ( ) as grouping brackets, not literal ones, unless you escape them.
# 3  
Old 10-02-2012
The nice thing about awk/perl is you don't actually have to make big giant do-everything regexes to handle the logic.
$ cat mmatch.awk

{ N=0 }
     (NF==3) && ($1 == "(-") && ($2 == "username") && /\)\\$/ { T=$0 ; getline; N=1 }
N && (NF==3) && ($1 == "(-") && ($2 == "username") && /\)$/ { print T; print }

$ awk -F"," -f mmatch.awk data



Last edited by Corona688; 10-02-2012 at 01:44 PM..
# 4  
Old 10-03-2012
Thank you for reply, sorry but I do not understand what you have done in awk script
# 5  
Old 10-03-2012
For each line that comes in:

1) Set N=0
2) If there's 3 tokens in the line, it begins with "(-", the second token is "username", and it ends with ")\", save the line in T, increment N, and get the next line.
3) If N is nonzero, the line has three tokens, begins with "(-", the second token is "username", and it ends with ")", print this line and the last.
# 6  
Old 10-03-2012
A slight change to your original awk should get the job done:

awk 'BEGIN {musr="[(]-,username,[^)][)]\\\\?"} $0~musr' netgroup

Two of the \ chars are stripped by the shell, leaving \\ for awk
# 7  
Old 10-05-2012
Originally Posted by Chubler_XL
A slight change to your original awk should get the job done:

awk 'BEGIN {musr="[(]-,username,[^)][)]\\\\?"} $0~musr' netgroup

Two of the \ chars are stripped by the shell, leaving \\ for awk
No, awk just needs an irrational amount of backslashes:

$ echo 'BEGIN {musr="[(]-,username,[^)][)]\\\\?"} $0~musr'

BEGIN {musr="[(]-,username,[^)][)]\\\\?"} $0~musr


This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Solaris

Application not working in multi core platform

Hi, I have a multiprocess C application (used POSIX library for threads and fork() & exec for creating process) of millions of LOC. 1. Which works fine in single processor machine. 2. Which works fine in multicore machine only if one core is enabled. Problem is, which results an undefined... (2 Replies)
Discussion started by: sreejesh
2 Replies

2. 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

3. UNIX for Advanced & Expert Users

Multi-platform Centralized Patch Management

We have a mix of AIX, HP-UX, Linux (RHEL and SLES), and Solaris in our environment. Currently we have seperate patch management systems for each platform (NIM, SD, Spacewalk, etc), but have started looking for a centralized patch management solution that would work for most, if not all, of our... (0 Replies)
Discussion started by: kknigga
0 Replies

4. Programming

Multi-platform includes?

I know that <cstudio> can also be <stdio> and can be written different ways on Linux then with windows. I've see some code doing a IFDEF __APPLE__ (I'm guessing, if compiled on a mac do whats between this) Is there one for Linux/Window? (3 Replies)
Discussion started by: james2432
3 Replies

5. UNIX for Dummies Questions & Answers

running a Perl script on HPUX platform

Hi, I wish to execute a simple perl script to pass unix commands on a HPUX platform, retrieve the result and filter through the text to determine outcomes x,y and z. I am developing the code on my windows system. I initially wrote the code to issue UNIX commands line by line, however i soon... (1 Reply)
Discussion started by: mmetcalfe
1 Replies
Login or Register to Ask a Question

Featured Tech Videos