LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-03-2007, 05:40 AM   #1
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Rep: Reputation: 15
awk program. a little query!


Hello All,

I have a small query.
I used the following awk command on the unix command line...

awk ' { print $0 } { FS = "( [[:digit:]]{4} )"; print $1; } ' coins.txt

The data in coins.txt is...

gold 1 1986 USA American Eagle
gold 1 1908 Austria-Hungary Franz Josef 100 silver 10 1981 USA ingot
gold 1 1984 Switzerland ingot
gold 1 1979 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand

The output should be:

gold 1 1986 USA American Eagle
gold 1
gold 1 1908 Austria-Hungary Franz Josef 100 Korona
gold 1
silver 10 1981 USA ingot
silver 10
gold 1 1984 Switzerland ingot
gold 1
gold 1 1979 RSA Krugerrand
gold 1
gold 0.5 1981 RSA Krugerrand
gold 0.5

But, it is...

gold 1 1986 USA American Eagle
gold
gold 1 1908 Austria-Hungary Franz Josef 100 Korona
gold 1
silver 10 1981 USA ingot
silver 10
gold 1 1984 Switzerland ingot
gold 1
gold 1 1979 RSA Krugerrand
gold 1
gold 0.5 1981 RSA Krugerrand
gold 0.5

i.e. : The data in red should be "gold 1" instead of just "gold".

Why is it like this?

Thanks in advance,

indiancosmonaut.
 
Old 07-03-2007, 06:15 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

The awk line shown doesn't look syntactically correct. Too my knowledge you cannot use the FS option in the main part (it should be in the BEGIN part or given to awk with the -F option).

BTW the input you show does not reflect the output examples: korona is nowhere to be found in the infile.....

Another thing: Is line 2 correct (gold 1 1908 Austria-Hungary Franz Josef 100 silver 10 1981 USA ingot) or should this one line be 2 lines. I.e:
gold 1 1908 Austria-Hungary Franz Josef 100
silver 10 1981 USA ingot

This will do what you want if line 2 are actually 2 lines:

awk '{ print $0 } { print $1, $2 }' coins.txt

Hope this helps.
 
Old 07-03-2007, 06:28 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
I guess that BEGIN {FS="[[:digit:]]{4}"} will not work.

Code:
cat coins
gold 1 1986 USA American Eagle
gold 1 1908 Austria-Hungary Franz Josef 100
silver 10 1981 USA ingot
gold 1 1984 Switzerland ingot
gold 1 1979 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand
jschiwal@hpamd64:~/testdir> awk 'BEGIN {FS="[[:digit:]]{4}"}{ print $0 "\n" $1,$2 }' coins
gold 1 1986 USA American Eagle
gold 1 1986 USA American Eagle
gold 1 1908 Austria-Hungary Franz Josef 100
gold 1 1908 Austria-Hungary Franz Josef 100
silver 10 1981 USA ingot
silver 10 1981 USA ingot
gold 1 1984 Switzerland ingot
gold 1 1984 Switzerland ingot
gold 1 1979 RSA Krugerrand
gold 1 1979 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand
jschiwal@hpamd64:~/testdir> awk 'BEGIN {FS="[0-9][0-9][0-9][0-9]"}{ print $0 "\n" $1 }' coins
gold 1 1986 USA American Eagle
gold 1
gold 1 1908 Austria-Hungary Franz Josef 100
gold 1
silver 10 1981 USA ingot
silver 10
gold 1 1984 Switzerland ingot
gold 1
gold 1 1979 RSA Krugerrand
gold 1
gold 0.5 1981 RSA Krugerrand
gold 0.5
 
Old 07-04-2007, 01:38 AM   #4
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Original Poster
Rep: Reputation: 15
Hi druuna, jschiwal

Thank you for responding to my query.

druuna,

You are correct. My apologies for writing the wrong infile content.

Its actually,

gold 1 1986 USA American Eagle
gold 1 1908 Austria-Hungary Franz Josef 100 Korona
silver 10 1981 USA ingot
gold 1 1984 Switzerland ingot
gold 1 1979 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand

jschiwal,

The following are working fine...

1. awk 'BEGIN {FS="[0-9][0-9][0-9][0-9]"}{ print $0 "\n" $1 }' coins
2. awk 'BEGIN {FS="[[:digit:]]{4}"}{ print $0 "\n" $1 }' coins
3. awk 'BEGIN {FS=" [[:digit:]]{4} "}{ print $0 "\n" $1 }' coins

The output:

gold 1 1986 USA American Eagle
gold 1
gold 1 1908 Austria-Hungary Franz Josef 100 Korona
gold 1
silver 10 1981 USA ingot
silver 10
gold 1 1984 Switzerland ingot
gold 1
gold 1 1979 RSA Krugerrand
gold 1
gold 0.5 1981 RSA Krugerrand
gold 0.5
----------------------------------------------------------------------

One more question:
In example.3. i have put a space before and after the [[:digit:]]{4}.
Do you see any difference in example 2 and 3.

Thanks a lot to both.

Best Regards,
indiancosmonaut
 
Old 07-04-2007, 02:02 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
what about this:
Code:
awk '{ print $0 }{print $1,$2}' "file"
output:
Code:
# ./test1.sh
gold 1 1986 USA American Eagle
gold 1
gold 1 1908 Austria-Hungary Franz Josef 100 silver 10 1981 USA ingot
gold 1
gold 1 1984 Switzerland ingot
gold 1
gold 1 1979 RSA Krugerrand
gold 1
gold 0.5 1981 RSA Krugerrand
gold 0.5
 
Old 07-04-2007, 02:33 AM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
The "BEGIN {FS="[[:digit:]]}" part does not work on my system.
Code:
awk 'BEGIN {FS="[[:digit:]]{4}"}{ print $1  }' coins
gold 1 1986 USA American Eagle
gold 1 1908 Austria-Hungary Franz Josef 100
silver 10 1981 USA ingot
gold 1 1984 Switzerland ingot
gold 1 1979 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand
Here I tried to just print the first field.
This works:
Code:
awk 'BEGIN {FS="[0-9][0-9][0-9]"}{ print $1  }' coins
gold 1
gold 1
silver 10
gold 1
gold 1
gold 0.5
Code:
awk --version
GNU Awk 3.1.5
...
 
Old 07-05-2007, 05:38 AM   #7
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Original Poster
Rep: Reputation: 15
Hi jschiwal,

Its is working on my system. I couldn't find the version of my awk!
Apparently it is 'POSIX friendly'.

Thanks a lot for the replies. They really helped! []

-----

Hi ghostdog74,

Actually i was looking to clear my concept on fieldseparator.
But thanks anyway.

Best Regards,
indiancosmonaut
 
Old 07-05-2007, 10:48 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
to use metacharacters and character lists in regular expression, use the --re-interval option. Or use --posix option

Last edited by ghostdog74; 07-05-2007 at 10:51 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using C API; first query passed passed by program to MySQL is successful, second quer weibullguy Programming 2 06-12-2007 11:45 PM
How add input value to some varieble in awk program this is probably funny for some of you but I do not know what to do. I have sarajevo Programming 2 05-25-2007 08:54 AM
Counting number of occurance in awk program sarajevo Programming 2 11-01-2006 11:31 AM
How to run a shell command containing awk, and grep within a C program Linh Programming 3 06-05-2003 08:05 PM
Running a shell command containing awk and grep within a C program Linh Programming 1 06-05-2003 07:51 PM


All times are GMT -5. The time now is 08:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration