LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 03-25-2010, 06:49 PM   #1
szahmad1
LQ Newbie
 
Registered: Nov 2009
Posts: 14

Rep: Reputation: 1
Unhappy about the "awk" command


i am reading a database flat file with the "awk" command which has 4 fields separated by colon ":" .I want to show the output of these fields in a certain way that every record is numbered e.g

1.some text
2.some text

Is there a way to do this?Any suggestions would be highly appreciated.
 
Old 03-25-2010, 06:55 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
I don't see the correlation between the question you asked (regarding the 4 fields separated by colons) and the desired output you showed in your example.

If you simply want to display a file, showing the contained colon-separated-values on a line-by-line basis, why won't `cat -n` work? Further, pipe the output of `cat -n` into awk of you want to do something more to it.

If I have missed the point, please explain more, perhaps with a more "realistic" example, showing the sort of output you desire, including how the output is to be numbered as well as how the "4 colon separated fields" are to be displayed.

Best regards,
Sasha

EDIT: P.S. perhaps I'm not understanding the initial format of this "flat file" so if that appears to be the case, please also describe the file, it is something other than some sort of ascii file.

Last edited by GrapefruiTgirl; 03-25-2010 at 06:57 PM.
 
Old 03-25-2010, 07:02 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
What Sasha said ... but in a generic manner:
Code:
awk '{print NR" "$0}' file
will print any record with its line-number.
 
Old 03-25-2010, 07:04 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
@ Tink -- perfect, toss the cat out thanks!
 
Old 03-25-2010, 07:12 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by GrapefruiTgirl View Post
@ Tink -- perfect, toss the cat out thanks!
Now there's an idiomatic phrase I hadn't come across
before ... what does it mean (in Nova Scotia?)?
 
Old 03-25-2010, 07:13 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Heh, no, there's no geographical significance -- simply what it says: no need to use `cat`
 
Old 03-25-2010, 07:16 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Oh ... I must be in too silly a mindset at the moment ;}

My apologies to the OP for the digression.
 
Old 03-25-2010, 08:06 PM   #8
szahmad1
LQ Newbie
 
Registered: Nov 2009
Posts: 14

Original Poster
Rep: Reputation: 1
Thanks for replies, here is the detail:
my original file looks like this:-
A100:Annie Marie's Dance Academy:123:1500
F110:Frankies Repair Shop:1123:250
S100:Sandra's Counselling Service:67:500
A100:Annie Marie's Dance Academy:254:500

and i want it look like this

1.A100:Annie Marie's Dance Academy:123:1500
2.F110:Frankies Repair Shop:1123:250
3.S100:Sandra's Counselling Service:67:500
4.A100:Annie Marie's Dance Academy:254:500

i think i am a lil clear now....lol
 
Old 03-25-2010, 08:09 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Well, in that case my micro-snippet is all you need; with a small modification.
Code:
awk '{print NR"."$0}' file
 
Old 03-25-2010, 08:09 PM   #10
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Perfectly clear

So, did you try what Tinkster suggested? (EDIT: ahh yes, the "." added in there)
 
Old 03-25-2010, 08:24 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Well spotted, Captain!
 
Old 03-25-2010, 08:42 PM   #12
szahmad1
LQ Newbie
 
Registered: Nov 2009
Posts: 14

Original Poster
Rep: Reputation: 1
This is the command: awk -F ":" '$1 ~ /'$custNum'/ {print NR".", $0;}{print $1,$2,$3,$4}' purchases.dat > temp
This is my output:
A100 Annie Marie's Dance Academy 123 1500
F110 Frankies Repair Shop 1123 250
3. S100:Sandra's Counselling Service:67:500
S100 Sandra's Counselling Service 67 500
A100 Annie Marie's Dance Academy 254 500

Its only numbering the record for which i am searching for
 
Old 03-25-2010, 08:45 PM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
And what is the desired result?
 
Old 03-25-2010, 08:48 PM   #14
szahmad1
LQ Newbie
 
Registered: Nov 2009
Posts: 14

Original Poster
Rep: Reputation: 1
Ok thanks for your help. I appreciate it
 
Old 03-25-2010, 09:04 PM   #15
jssidhu4
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
i knew this site is cool....i can get help here i love this open source LIVE LONG LINUX
 
  


Reply



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
printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! mayankmehta83 Linux - Newbie 2 12-03-2009 02:55 AM
removing "\r" while using command cp , grep & awk rakesh.tandur Linux - Newbie 4 05-09-2008 06:02 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
help with "next" command in "awk" realos Programming 2 05-13-2003 03:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:06 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
Open Source Consulting | Domain Registration