LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-23-2007, 03:09 PM   #1
jumper_bl
LQ Newbie
 
Registered: Nov 2007
Posts: 2

Rep: Reputation: 0
Need some help with awk


I am working with data which is in this format:

Name1
Product1
Cost
Inventory #

Name2
Product2
Cost
Inventory #

What I want to do is conver that file into

Name1,Product1, Cost, Inventory
Name2,Product2, Cost, Inventory

How should I do this?

Any help is appreciated.
 
Old 11-23-2007, 03:33 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

Something like this: awk 'BEGIN { FS="\n" ; RS="" } { print $1","$2","$3","$4 }' infile

Code:
$ cat infile
Name1
Product1
Cost 1.00
Inventory # 1

Name2
Product2
Cost 2.00
Inventory # 20

Name3
Product3
Cost 3.00
Inventory # 300

Name4
Product4
Cost 4.00
Inventory # 400


$ awk 'BEGIN { FS="\n" ; RS="" } { print $1","$2","$3","$4 }' infile
Name1,Product1,Cost 1.00,Inventory # 1
Name2,Product2,Cost 2.00,Inventory # 20
Name3,Product3,Cost 3.00,Inventory # 300
Name4,Product4,Cost 4.00,Inventory # 400
You can change the field separator (FS) and/or the record separator (RS) and manipulate the way the data is read.

Hope this helps.
 
Old 11-23-2007, 03:48 PM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Not pure awk, but I'd do it this way:
Code:
sed '/^$/d' /path/to/file | awk 'BEGIN { RS="\nInventory #\n"; FS="\n" } { for (i=1; i<=NF; i++) { printf $i; if (i != NF) printf ", " }; printf "\n" }'
which gives
Code:
[0 dave@cronus ~]$ cat go
Name1
Product1
Cost
Inventory #

Name2
Product2
Cost
Inventory #
[0 dave@cronus ~]$ sed '/^$/d' go | awk 'BEGIN { RS="\nInventory #\n"; FS="\n" } { for (i=1; i<=NF; i++) { printf $i; if (i!=NF) printf ", " }; printf "\n" }'
Name1, Product1, Cost
Name2, Product2, Cost
[0 dave@cronus ~]$
Dave

Last edited by ilikejam; 11-23-2007 at 03:50 PM.
 
Old 11-23-2007, 03:52 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

@ilikejam: inventory # should be part of the output.....
 
Old 11-23-2007, 03:56 PM   #5
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Oh yeah. Right you are.
Code:
sed '/^$/d' /path/to/file | awk 'BEGIN { RS="#\n"; FS="\n" } { for (i=1; i<=NF; i++) { printf $i; if (i!=NF) printf ", " }; printf "\n" }'
 
Old 11-23-2007, 04:49 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
And another awk ...
Code:
awk 'BEGIN {FS="\n";OFS=",";RS=""}{$1=$1;print} ' test
 
Old 11-23-2007, 05:00 PM   #7
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Clever. Someone always has a shorter way of doing things than me.

Maybe I should change my username to 'verbose'...
 
Old 11-23-2007, 05:07 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
@ilikejam: On what platform did you try out your commands? I'm asking because they don't work here. The output is one long line, no linefeed in between the individual lines:

Code:
$ sed '/^$/d' infile | awk 'BEGIN { RS="\nInventory #\n"; FS="\n" } { for (i=1; i<=NF; i++) { printf $i; if (i!=NF) printf ", " }; printf "\n" }'
Name1, Product1, Cost 1.00, Inventory # 1, Name2, Product2, Cost 2.00, Inventory # 20, Name3, Product3, Cost 3.00, Inventory # 300, Name4, Product4, Cost 4.00, Inventory # 400, 
$
 
Old 11-23-2007, 05:11 PM   #9
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
That's 'cause you've got 'Inventory # 300' instead of just 'Inventory #'. It's not picking up the record separator properly.

It's not entirely clear how the OP's data is actually formatted.

Dave

Last edited by ilikejam; 11-23-2007 at 05:12 PM.
 
Old 11-23-2007, 05:20 PM   #10
jumper_bl
LQ Newbie
 
Registered: Nov 2007
Posts: 2

Original Poster
Rep: Reputation: 0
thanks that works..

Quote:
Originally Posted by druuna View Post
Hi,

Something like this: awk 'BEGIN { FS="\n" ; RS="" } { print $1","$2","$3","$4 }' infile

Code:
$ cat infile
Name1
Product1
Cost 1.00
Inventory # 1

Name2
Product2
Cost 2.00
Inventory # 20

Name3
Product3
Cost 3.00
Inventory # 300

Name4
Product4
Cost 4.00
Inventory # 400


$ awk 'BEGIN { FS="\n" ; RS="" } { print $1","$2","$3","$4 }' infile
Name1,Product1,Cost 1.00,Inventory # 1
Name2,Product2,Cost 2.00,Inventory # 20
Name3,Product3,Cost 3.00,Inventory # 300
Name4,Product4,Cost 4.00,Inventory # 400
You can change the field separator (FS) and/or the record separator (RS) and manipulate the way the data is read.

Hope this helps.
 
Old 11-23-2007, 05:24 PM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Quote:
Originally Posted by ilikejam View Post
That's 'cause you've got 'Inventory # 300' instead of just 'Inventory #'. It's not picking up the record separator properly.

It's not entirely clear how the OP's data is actually formatted.

Dave
True. On both accounts
 
  


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
awk help uttam_h Programming 9 10-16-2007 10:32 AM
help with awk sang_froid Programming 1 07-05-2007 01:35 PM
awk kalyanofb Programming 4 02-19-2007 01:55 AM
Do you know awk? Vitalka Linux - General 2 12-01-2006 11:53 PM
using awk meniscus Linux - Newbie 6 10-05-2006 12:39 PM


All times are GMT -5. The time now is 10:49 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration