LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 03-18-2015, 09:39 PM   #1
kevz
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Rep: Reputation: Disabled
Add n characters at end of line depending on character count for line


Hello,

I have a flat file where I expect to have 5 values delimited by 4 commas ",' :
agf,sdya,geg,fgd,gdfgr

but sometimes I have the following:
agf,sdya,geg,fgd
agf,sdya,geg
agf,sdya
agf

For those lines, I wanna append commas at the end of the lines in order to always have a total of 4 commas:
agf,sdya,geg,fgd,
agf,sdya,geg,,
agf,sdya,,,
agf,,,,

The following awk command already gives me the count number of "," for each line:
awk -F\, '{print NF-1}' "MyFile"

But I am not sure where to go from there.
Basically I want to do the following;
If CommaCount For CurrentLine != 4 Then Append(4 - "number of commas found')CommasToLine
Else CheckNextLine.

Thanks for your help !
 
Old 03-18-2015, 10:23 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
How about:
Code:
awk -F, 'NF=5' OFS="," MyFile
 
2 members found this post helpful.
Old 03-18-2015, 10:40 PM   #3
kevz
LQ Newbie
 
Registered: Mar 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
wow, gotta learn awk !
It looks so simple =)
Thank you !
 
Old 03-19-2015, 12:54 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Let's hope there's never more than 5 fields on input ...
 
Old 03-19-2015, 01:18 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well I was only going to the requirements set
 
  


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
[SOLVED] sed command for matching the end character of a line and replacing the whole line. mamunm Linux - Newbie 4 04-12-2014 12:19 AM
[SOLVED] Adding a character in each line depending upon number of such characters present in a line. ls_milkyway Linux - Newbie 13 08-24-2013 03:19 AM
sed add a character to the end of each line keenboy Linux - General 2 08-05-2010 12:36 PM
End-of-line Characters missing from last line of md5 file. Md5sum fails mehorter Linux - General 5 06-29-2009 08:56 PM
Shell scripting: How to add characters at the end of the line Micro420 Programming 7 05-18-2007 01:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:15 AM.

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