Using a multi-character field separator in awk on Solaris

2

I wish to use a string (BIRCH) as a field delimiter in awk to print second field. I am trying the following command:

cat tmp.log|awk -FBirch '{ print $2}'

Below output is getting printed:

irch2014/06/23,04:36:45,3,1401503,xml-harlan,P12345-1,temp,0a653356353635635,temp,L,Success

Desired output:

2014/06/23,04:36:45,3,1401503,xml-harlan,P12345-1,temp,0a653356353635635,temp,L,Success

Contents of tmp.log file.

-bash-3.2# cat tmp.log  
Dec 05 13:49:23 [x.x.x.x.180.100] business-log-dev/int [TEST][0x80000001][business-log][info] mpgw(Test): trans(8497187)[request][10.x.x.x]:
Birch2014/06/23,04:36:45,3,1401503,xml-harlan,P12345-1,temp,0a653356353635635,temp,L,Success

Am I doing something wrong?

  • OS: Solaris10
  • Shell: Bash

Tried below command suggested in one of the ansers below. I am getting the desired output, but with an extra empty line at the top. How can this be eliminated from the output?

-bash-3.2# /usr/xpg4/bin/awk -FBirch '{print $2}' tmp.log

2014/06/23,04:36:45,3,1401503,xml-harlan,P12345-1,temp,0a653356353635635,temp,L,Success

bash
shell
awk
solaris
asked on Stack Overflow Dec 10, 2014 by user2607367 • edited Dec 10, 2014 by user2607367

2 Answers

3

Originally, I suggested putting quotes around "Birch" (-F'Birch') but actually, I don't think that should make any difference.

I'm not at all experienced working with Solaris but you may want to also try using nawk ("new awk") instead of awk.

nawk -FBirch '{print $2}' file

If this works, you may want to consider creating an alias so that you always use the newer version of awk with more features.

You may also want to try using the version of awk in the /usr/xpg4/bin directory, which is a POSIX compliant implementation so should support multi-character FS:

/usr/xpg4/bin/awk -FBirch '{print $2}' file

If you only want to print lines which have more than one field, you can add a condition:

/usr/xpg4/bin/awk -FBirch 'NF>1{print $2}' file

This only prints the second field when there is more than one field.

answered on Stack Overflow Dec 10, 2014 by Tom Fenech • edited Dec 10, 2014 by Tom Fenech
1

From the man page of the default awk on solaris usr/bin/awk

-Fc            Uses the character c as the  field  separator
               (FS)  character.   See  the  discussion of FS
               below.

As you can see solaris awk only takes a single character as a Field separator

Also in the man page is split

split(s, a, fs)

     Split the string s into array elements a[1],  a[2],  ...
     a[n],  and  returns  n.  The separation is done with the
     regular expression fs or with the field separator FS  if
     fs is not given.

As you can see here it takes a regular expression as a separator so we can use.

awk 'split($0,a,"Birch"){print a[2]}' file

To print the second field split by Birch

answered on Stack Overflow Dec 10, 2014 by (unknown user) • edited Dec 10, 2014 by (unknown user)

User contributions licensed under CC BY-SA 3.0