LinuxQuestions.org
Review your favorite Linux distribution.
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 04-11-2004, 10:31 AM   #1
fooforon
Member
 
Registered: Sep 2003
Posts: 37

Rep: Reputation: 15
Awk output Trouble


Hi guys,

I'm having problem's with the output of awk I have a few things to say first ill show the database file layout

FILE :

#Groupname; Lastname, Firstname
*******************************
amotet; Tetzlaff, Nicholas
lavlym; Lavallie, Stacey Anne
lawzuz; Zuzarte, Mark W
lawzuz; Lawrence Leger, Daniel Thomas
leusom; Somji, Rahim
leusom; Leung, Hoi-yung Gary
lavlym; Lymberis, Andrew John

NEEDED OUTPUT:

Team Member ---------------------------------------- Group Name
=========================================
Stacey Anne Lavallie -------------------------------------lavlym
Andrew John Lymberis-----------------------------------lavlym

-- suppose to be white space

I have the user input the groupname they would like to see the members for this i just input the string manually

I'm having Problems with the spacing

sed 's/,/;/g' teamlist.dat > test
awk 'FS=";" {if ($1 ~/lavlym/ && NR > 2) print $3, $2, $1}' test
outputs this

Team Member ---------------------------------------- Group Name
=========================================
Stacey Lavallie lavlym
Andrew Lymberis lavlym

it chops off there middle name but is I only display $3 it show first and middle like its suppose to

If anyone can point me in the right direction I would greatly appreciate it
Thanks

Jamie

Last edited by fooforon; 04-11-2004 at 11:31 AM.
 
Old 04-11-2004, 11:36 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Would something like this work for you?

awk '/lavlym/ { print $3, $2, ": ", $1}' teamlist.dat \
| sed -e 's/, //' -e 's/;//'
Stacey Lavallie: lavlym
Andrew Lymberis: lavlym
 
Old 04-11-2004, 11:55 AM   #3
fooforon
Member
 
Registered: Sep 2003
Posts: 37

Original Poster
Rep: Reputation: 15
Unhappy

Thanks for the input but that still cuts on there second part of there first name
 
Old 04-11-2004, 02:08 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
awk ' { print $3, $4, $2, ": ", $1}' teamlist.dat | sed -e 's/, //' -e 's/;//'
Firstname Lastname: #Groupname
: *******************************
Nicholas Tetzlaff: amotet
Stacey Anne Lavallie: lavlym
Mark W Zuzarte: lawzuz
LegerDaniel Lawrence : lawzuz
Rahim Somji: leusom
Hoi-yung Gary Leung: leusom
Andrew John Lymberis: lavlym
 
Old 04-11-2004, 06:37 PM   #5
fooforon
Member
 
Registered: Sep 2003
Posts: 37

Original Poster
Rep: Reputation: 15
That works for most case but I forgot to show that some people have double first names and double last name so default FS wont work

I have splite fine I need to format it now
it is odd

awk -v y=$temp 'FS=";" {if ($1 ~y && NR > 2) printf "%s%s%35s\n", $3, $2, $1 }' test

if I do that It only shows the first 8 chars of $3 and if I put any spaces after like this

awk -v y=$temp 'FS=";" {if ($1 ~y && NR > 2) printf "%s %s%35s\n", $3, $2, $1 }' test

it prints infront of $3 im confused by this buy any other space after %35s it goes after $2 so I dont know why this is happerning
 
Old 04-12-2004, 12:28 AM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. As you're quickly discovering, tools like "awk" and "sed" work best when
your data format is simple; they become much more difficult when there are
a lot of "exceptions to the rule".

If you don't have a really, really simple data format, you're often much better
off writing in some other tool (Perl, for example, or C). Please consider if this
project might be better done in Perl than in "awk".

2. Having said that, the key to solving this problem in "awk" is to "simplify" the
format. We have three fields ("group", "last name" and "first name"). If we
reduce things to just one delimiter, then it doesn't matter whether a "last name"
has one, two, three or more words - it all becomes a single field.

For example:
sed 's/;/,/' teamlist.dat \
| awk -F',' '{ printf "%s%s %-10s\n", $3, $2, $1 }' # Parse/format the line

'Hope that helps .. PSM
 
Old 04-12-2004, 07:14 AM   #7
fooforon
Member
 
Registered: Sep 2003
Posts: 37

Original Poster
Rep: Reputation: 15
Thanks, that helps better, I would use something different but it is a school assignment for shell scripting course, which we use bash mostly so kinda stuck with what I have thanks for all your input though



Jamie
 
  


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
Via AC'97 5.1 Optical Output or Audigy 4.1 Output Nza Fedora 3 06-01-2004 07:49 AM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM
using awk wedgeworth Linux - Newbie 9 02-20-2004 07:48 AM
How do I zip and attach the output data of a grep | awk | mail shell script? 360 Programming 1 05-08-2002 08:26 AM
Awk and Shell CMD Output xanthium Programming 16 04-24-2002 06:13 AM

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

All times are GMT -5. The time now is 12:28 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