Awk expressions working & not working


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Awk expressions working & not working
# 1  
Old 10-12-2011
Awk expressions working & not working

Hi,

Putting across a few awk expressions.
Apart from the last, all of them are working.

Code:
echo a/b/c | awk -F'/b/c$' '{print $1}'
a

echo a/b/c++ | awk -F'/b/c++' '{print $1}'
a

echo a/b/c++ | awk -F'/b/c++$' '{print $1}'
a/b/c++

Request thoughts on why putting a '$' post double ++ changed everything.
I require a $ as interested in only the last string.

Do let me know workarounds to accomplish the same.
Thanks in advance

Last edited by pludi; 10-12-2011 at 05:50 AM..
# 2  
Old 10-12-2011
In certain positions the + sign is special (one or more occurrences of the previous character):

Code:
% echo a/b/c++ | awk -F'/b/c\\++$' '{print $1}'
a

# 3  
Old 10-12-2011
Yes thought of the same but couldn't fit that logic here.

If + is working based on its logic of more occurrences, it shouldn't have worked without the $.
Wondering what effect has putting the $ has?
# 4  
Old 10-12-2011
/b/c++ matches / - b - / - c (one or more occurrences) - + (this one seems to be ignored)

So:

Code:
% echo a/b/c+ | awk -F'/b/c++' '{print $1, $2}'
a +

Code:
% echo a/b/cccc+ | awk -F'/b/c++' '{print $1, $2}'
a +

/b/c++$ matches / - b - / - c (one or more occurrences) - + (ignored) at the end of the string ($):

These two should be clear:

Code:
% echo a/b/c++ |
  awk -F'/b/c++$' '{
    print "NF:", NF
    print "$1:", $1
    print "$2:", $2
    }'
NF: 1
$1: a/b/c++
$2: 
% echo a/b/c++ |
  awk -F'/b/c++' '{ 
    print "NF:", NF
    print "$1:", $1
    print "$2:", $2
    }'         
NF: 2
$1: a
$2: ++

Here the pattern (FS) is not found:

Code:
% echo a/b/c++ | 
  awk -F'/b/c++$' '{
    print "NF:", NF
    print "$1:", $1
    print "$2:", $2
    }'                            
NF: 1
$1: a/b/c++
$2:

I suppose that the behaviour is unspecified for more than one consecutive quantifiers (+ signs), it siply ignores the second + sign:

Code:
% echo a/b/c |  
  awk -F'/b/c++$' '{
    print "NF:", NF
    print "$1:", $1
    print "$2:", $2
    }'                          
NF: 2
$1: a
$2:

Here only the last a/b/c[c..] matches, because it's at the end of the record:

Code:
% echo a/b/ca/b/ccc |
  awk -F'/b/c++$' '{
    print "NF:", NF
    print "$1:", $1
    print "$2:", $2
    }'                                 
NF: 2
$1: a/b/ca
$2:



This may be implementation specific (I've used GNU awk 4.0 and nawk version 20070501).

Last edited by radoulov; 10-12-2011 at 08:07 AM..
# 5  
Old 10-12-2011
I found this in SUS:


Code:
9.4.6 EREs Matching Multiple Characters
[...]
The behavior of multiple adjacent duplication symbols ( '+', '*', '?', and intervals) produces undefined results.

# 6  
Old 10-12-2011
Thanks a ton.
I thought myself to be good with regular expressions, probably time to change it Smilie

Let's assume below stands true:
Quote:
The behavior of multiple adjacent duplication symbols ( '+', '*', '?', and intervals) produces undefined results.
echo a/b/c+ | awk -F'/b/c+' '{print $1, $2}'
a +
Quote:
c+ matches one or more occurrences of 'c'. + comes as second field
echo a/b/c | awk -F'/b/c+' '{print $1, $2}'
a
Quote:
We don't have a second field here
Request your thoughts on why the below doesn't match when we have a $:
echo a/b/c+ | awk -F'/b/c+$' '{print $1, $2}'
a/b/c+
Quote:
Here is my reasoning, please correct
c+ matches a/b/c.
$ makes it to think that it's the end of line, but it isn't as we have an additional + after that.
Hence, it doesn't qualify for a match & fails.
# 7  
Old 10-12-2011
Quote:
Originally Posted by vibhor_agarwali
Thanks a ton.
[...]
Request your thoughts on why the below doesn't match when we have a $:
echo a/b/c+ | awk -F'/b/c+$' '{print $1, $2}'
a/b/c+


c+ matches a/b/c.
$ makes it to think that it's the end of line, but it isn't as we have an additional + after that.
Hence, it doesn't qualify for a match & fails.
Exactly (IMHO).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Disk Space Utilization in HTML format working in one environment and not working on the other

Hi Team, I have written the shell script which returns the result of the disk space filesystems which has crossed the threshold limit in HTML Format. Below mentioned is the script which worked perfectly on QA system. df -h | awk -v host=`hostname` ' BEGIN { print "<table border="4"... (13 Replies)
Discussion started by: Harihsun
13 Replies

2. Shell Programming and Scripting

Working web service call not working with curl

Hello, Newbie here, I have a perfectly well working web service call I can issue from chrome (PC Windows 10) and get the results I want (a dimmer being turned on in Fibaro Home Center 2 at level 40) I am not allowed to post urls but the below works with http and :// and... (3 Replies)
Discussion started by: abigbear
3 Replies

3. Shell Programming and Scripting

Automating pbrun /bin/su not working, whenever manually it is working using putty

I am trying to automate a script where I need to use pbrun /bin/su but for some reason it is not passing thru the pbrun as my code below. . ~/.bash_profile pbrun /bin/su - content c h 1 hpsvn up file path I am executing this from an external .sh file that is pointing to this scripts file... (14 Replies)
Discussion started by: jorgejac
14 Replies

4. Red Hat

Nslookup working but ping not working at windows client

Hi Team we have created a DNS server at RHEL6.2 environment in 10.20.203.x/24 network. Everything is going well on linux client as nslookup, ping by host etc in entire subnet. We are getting problem in windows client as nslookup working as well but not ping. all the firewall is disabled and... (5 Replies)
Discussion started by: boby.kumar
5 Replies

5. Shell Programming and Scripting

>& redirection not working within csh script

I'm having a strange problem with basic >& output redirection to a simple log file in csh. When I run this particular output redirection on the command line, it works, but then when I run the same output redirection command >& in my c shell script, I get a blank log file. Nothing is output to the... (5 Replies)
Discussion started by: silencio
5 Replies

6. Shell Programming and Scripting

awk & zcat not working together

I have a tar file which i want to read and check some specific fields basis on which i want to get output. Code zcat samplefile.tar.gz | awk 'FNR==1 {++counter} counter ==2 {BB=1;next} substr($0,26,2) =="01") {next} (substr($0,28,12) ~ "^") {next} (substr($0,184,3) in BB) {next} 1 '... (7 Replies)
Discussion started by: siramitsharma
7 Replies

7. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

8. Emergency UNIX and Linux Support

SSH Is hanging and the & sign does not seem to be working

I am having an issue where I am do an SSH to about 30 servers one at a time however my script is getting hung up sometimes on the SSH. I thought the & at the end as seen below would fire it and move on but that does not seem to be working. #!/bin/sh for remsys in trumpetsnail angel... (15 Replies)
Discussion started by: LRoberts
15 Replies

9. Shell Programming and Scripting

bash with: if, elif & regex not working

Why is only hello3 being printed? There must be some kind of syntax problem because the file list definitely includes all the file extensions line by line. #!/bin/bash find '/home/myuser/folder/' -name '*.c' -type f | while read F do if ] # if the file name ends in .txt.c then ... (6 Replies)
Discussion started by: cyler
6 Replies

10. Solaris

OS Problems -no DNS & SSH not working

I just installed Solaris 6/10 without any problems but I didn't connect the network cable when I installed it. Here are my problems: -I can access webpages using IP addrsses but not with domain names -ssh is installed but it is not running ('ps -e | grep sshd' didn't show it) I have been... (4 Replies)
Discussion started by: kungpow
4 Replies
Login or Register to Ask a Question