LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-12-2009, 02:17 AM   #1
tribal_molecule
LQ Newbie
 
Registered: May 2007
Location: india
Distribution: slackware
Posts: 4

Rep: Reputation: 0
Count commas in header and append as many missing from the lines underneath


I have a data file like this -

A,B,C,D,E,
1,2,3,4,5,
1,2,3
1,2,3,4
1,2,3,4,5,


So, the header record is the perfect record. The first record after that is also the perfect record.
The next 2 records are not.

What i have been trying to unsuccessfully do through a script is that when I run the script, my output should look like this -

A,B,C,D,E,
1,2,3,4,5,
1,2,3,,,
1,2,3,4,,
1,2,3,4,5,

It's like the difference in number of commas are appended to the lines which do not have exact number of commas as that of the header.

Please try and give me the ideas or if anyone could possibly write a code snippet and tell me.

Thanks,
Ak
 
Old 08-12-2009, 06:05 AM   #2
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
if you have this already in a file, say input.txt
then:

sed 's?^[^,]*,[^,]*,[^,]*,[^,]*$?&,,?' input.txt | \
sed 's?^[^,]*,[^,]*,[^,]*$?&,,,?' | \
sed 's?^[^,]*,[^,]*,[^,]*$?&,,,?'

if you got it from stdout, you can just pipe the result:

... | \
sed 's?^[^,]*,[^,]*,[^,]*,[^,]*$?&,,?' | \
sed 's?^[^,]*,[^,]*,[^,]*$?&,,,?' | \
sed 's?^[^,]*,[^,]*,[^,]*$?&,,,?'
 
  


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
script to count lines kalimat Programming 16 12-15-2008 04:09 AM
count lines beginning with 50 chloraldo Programming 3 08-18-2006 08:30 AM
command to count how many lines in a file? dr_zayus69 Linux - Software 3 01-06-2005 10:42 AM
Help, LILO append lines. BmxFace Linux - General 1 10-27-2003 05:51 AM
Append lines? HELP! jimmytango829 Linux - Hardware 1 07-07-2003 11:06 PM

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

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