LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Add n characters at end of line depending on character count for line (https://www.linuxquestions.org/questions/linux-newbie-8/add-n-characters-at-end-of-line-depending-on-character-count-for-line-4175537189/)

kevz 03-18-2015 09:39 PM

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 ! :)

grail 03-18-2015 10:23 PM

How about:
Code:

awk -F, 'NF=5' OFS="," MyFile

kevz 03-18-2015 10:40 PM

wow, gotta learn awk !
It looks so simple =)
Thank you !

syg00 03-19-2015 12:54 AM

Let's hope there's never more than 5 fields on input ... :tisk:

grail 03-19-2015 01:18 AM

Well I was only going to the requirements set :)


All times are GMT -5. The time now is 07:24 PM.