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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
11-23-2007, 03:09 PM
#1
LQ Newbie
Registered: Nov 2007
Posts: 2
Rep:
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.
11-23-2007, 03:33 PM
#2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
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.
11-23-2007, 03:48 PM
#3
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
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 .
11-23-2007, 03:52 PM
#4
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Hi,
@ilikejam: inventory # should be part of the output.....
11-23-2007, 03:56 PM
#5
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
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" }'
11-23-2007, 04:49 PM
#6
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
And another awk ...
Code:
awk 'BEGIN {FS="\n";OFS=",";RS=""}{$1=$1;print} ' test
11-23-2007, 05:00 PM
#7
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
Clever. Someone always has a shorter way of doing things than me.
Maybe I should change my username to 'verbose'...
11-23-2007, 05:07 PM
#8
LQ Veteran
Registered: Sep 2003
Posts: 10,532
@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,
$
11-23-2007, 05:11 PM
#9
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
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 .
11-23-2007, 05:20 PM
#10
LQ Newbie
Registered: Nov 2007
Posts: 2
Original Poster
Rep:
thanks that works..
Quote:
Originally Posted by
druuna
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.
11-23-2007, 05:24 PM
#11
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Quote:
Originally Posted by
ilikejam
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
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
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
LinuxQuestions.org
> Forums
> Linux Forums
> Linux - Newbie
All times are GMT -5. The time now is 04:49 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News