1827834 Members
1908 Online
109969 Solutions
New Discussion

Re: awk question

 
SOLVED
Go to solution
Aashish Raj
Valued Contributor

awk question

Hi,
I have a file where fields are seprated by ** .
I need to use awk with -F option to select the second field.
If the fields are seprated by *, i am able to select by using -F'*' but for **, i have tried different combinations but nothing is working.I tried -F"**" , -F'**' , -F\*\* but no luck.

Thanks
AR
8 REPLIES 8
Thierry Poels_1
Honored Contributor

Re: awk question

Hi,

the field separator is supposed to be one character only. So if "**" is used in the input file, use "*" as field separator, and skip those empty fields between the double "*".

good luck,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Robin Wakefield
Honored Contributor
Solution

Re: awk question

Hi,

I'd probably set FS in this instance:

awk 'BEGIN{FS="\*\*"}{print $3}' filename

Rgds, Robin
Steve Steel
Honored Contributor

Re: awk question

Hi


This worked as a quick test


echo 1**2|awk -F "[*][*]" '{print $2}'



Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Deepak Extross
Honored Contributor

Re: awk question

First run
sed 's/\*\*/\|/g'
to replace the "**" delimiter with "|".
Then run awk using -F\|

HTH.
Carlos Fernandez Riera
Honored Contributor

Re: awk question

awk -F '\\\*\\\*' ' { print $1}' file1
unsupported
Ceesjan van Hattum
Esteemed Contributor

Re: awk question

I go with Robin:

$> cat testfile
11**22**33
44**55**66
$> awk '{FS="\*\*"}{print $2}'testfile
22
55

Regards,
Ceesjan
Heiner E. Lennackers
Respected Contributor

Re: awk question

Hi,

awk expects as -F argument a regular expression. Because * is a multiplier in a regex, you have to define it as a character class.
e.g:
echo 1**2 | awk -F '[*][*]' '{print $2}'

echo 1**2 | awk -F '[*]{2}' '{print $2}'

Heiner
if this makes any sense to you, you have a BIG problem
SHABU KHAN
Trusted Contributor

Re: awk question

Hi Aashish,

You tried F"**" , -F'**' , -F\*\* but you didn't try -F'\\**'

Try two backslashes for two ** like this

ON HP's
echo "1**2**3**4" | awk -F'\\**' '{print $3,$4}'

use nawk on Sun's with the same syntax
Thanks,
Shabu