Compatible awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compatible awk command
# 1  
Old 04-11-2017
Compatible awk command

the following command works beautifully. it basically grabs the content of a file from First to End, then excludes any lines containing specific patterns.

Code:
awk '/^First/,/^End$/ {if (!/#\/bin\/sh|not.*commonly|#/) print}'  datafile.

i never had any issues with the command until I ran it on a Ubuntu box 13.04. when i ran it against a datafile, I got back no response. Nothing was outputted to the screen. It wasnt until I installed gawk on the system that the command started to work.

can anyone help me rewrite this command to make it more portable with default versions of awk? i dont want to have to install gawk. this script will be run on a a client machine and they dont like having to install anything on their system because they fear it'll break something else.

Last edited by SkySmart; 04-11-2017 at 04:37 PM..
# 2  
Old 04-11-2017
try this - some awk-s are finicky...:
Code:
awk '/^First/,/^End$/ {if ((!/#\/bin\/sh)|(not.*commonly/)|(/#/)) print}'  datafile.

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-11-2017
ubuntu uses mawk by default, which is faster but finicky.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 04-11-2017
Code:
awk '/^First/,/^End$/ {if (!/#\/bin\/sh|not.*commonly|#/) print}' file

is equivalent to:
Code:
awk '/^First/,/^End$/ {if (!/not.*commonly|#/) print}' file

I do not see how mawk or any awk would have a problem with that...

Could you post a sample where it goes wrong?


--
Quote:
Originally Posted by vgersh99
try this - some awk-s are finicky...:
Code:
awk '/^First/,/^End$/ {if ((!/#\/bin\/sh)|(not.*commonly/)|(/#/)) print}'  datafile.

This would have a different meaning and the syntax is incorrect..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-12-2017
it doesnt look like awk is going to be reliable here. but sed seems to be.

i found this command online:

Code:
sed -n '/ABC/,/^[^+]/{x;/^$/!p;}' file

and i modified it to look like this:

Code:
sed -n '/^First/,/^End$/ {x ; /#\/bin\/sh|not.*commonly|#/!p;}'

which seems to work. but dont know if im reading the command correctly. i take it, im storing everything between and including lines from "First" to "End" to variable x. then, im only printing lines from variable x which do not contain the strings
Code:
#\/bin\/sh|not.*commonly|#

# 6  
Old 04-12-2017
/#something|#/ is equal to /#/
Code:
awk '/^First/,/^End$/ {if (!/not.*commonly|#/) print}' datafile

The same with Unix sed (as always: multi-line works around bugs)
Code:
sed -n '
/^First/,/^End$/{
  /not.*commonly/b
  /#/b
  p
}
' datafile


Last edited by MadeInGermany; 04-12-2017 at 08:10 AM.. Reason: code tags added
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Vim compatible

I have ubuntu 32 bit installed on VMware with win 7 as host operating system. I am a newbie to Linux shell mode and vi editor. When ever I open the vi editor and select insert mode I don't see the insert mode being highlighted at the bottom of the screen as it is supposed to be. Only when I enable... (4 Replies)
Discussion started by: sabsac
4 Replies

2. Shell Programming and Scripting

Looking for a perl-compatible regex solution

This is for PHP preg_match code - which is PCRE therefore looking for a perl compatible suggestion I have this line returned I want to match and return.. I want to match the two instances of string ending 'ABCXYZ' into an array. And on second element (ie. RootABCXYZ) only return the word... (4 Replies)
Discussion started by: deadyetagain
4 Replies

3. Hardware

Trying to find a compatible OS for an old computer

Hello A friend and I recently got our hands on an old computer (a rainbow '83 I think), cleaned it, etc... And now we're trying to get the thing running. The problem is that the included OS is badly documented, so I thought of installing an old version of UNIX on it. So the questions are: Is that... (8 Replies)
Discussion started by: ijiboom
8 Replies

4. Hardware

is rhel 6 compatible with dell xps 14

is rhel 6 compatible with dell xps 14.I tried to install it but could not install.Installation hanged at detecting hardware (1 Reply)
Discussion started by: shamapraveen
1 Replies

5. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

6. UNIX for Dummies Questions & Answers

winzip compatible command

Dear friends, I m new to Unix, can anybody please guide me on how to zip and password protect a normal text (or any file) in unix prompt? (1 Reply)
Discussion started by: topgear1000cc
1 Replies

7. Solaris

Compatible NICs for Solaris 9 (9/05) x86

Hi, Please can someone recommend me a cheap PCI Network card for Solaris 9 (9/05) for an Intel based PC (x86). I currently have a US Robotics NIC installed, but Solaris does not recognise it and its been too much hassle to find drivers for it etc, so have decided to purchase another NIC that... (3 Replies)
Discussion started by: wajidc
3 Replies

8. Linux Benchmarks

Gd2 not compatible with Clientexec

Dear Frnds We are having WHM Cpanel in which many website are their , one of our customer want to install Clientexec software which requires GD2.0.28(compatible) library which is installed. The problem is libpng version which is incompatible with the GD library . Please kindly... (0 Replies)
Discussion started by: naik_mit
0 Replies

9. Linux

VPN compatible with NAT

Hey, i was wondering if anybody knew of a good VPN that is compatible with nat? If a VPN will not work with NAT could it work with PAT? Right now i'm using RH9. I wanted to use IPsec but i don't think that is compatiable with nat or pat. Please correct me if i'm wrong. Thanks, Byblyk. (0 Replies)
Discussion started by: byblyk
0 Replies

10. UNIX for Dummies Questions & Answers

Print Servers compatible with Solaris

Does anyone have any advice on what print servers to use with Solaris I used to use intel but they dicontinued the model we used. I would just use lpadmin to set them up . Any advice on this would be very helpful. (2 Replies)
Discussion started by: xmildx
2 Replies
Login or Register to Ask a Question