2

I have a file say FILE1.txt contains below data:-

A
B
C
D
E
F
G
H
I
J
K
L

I need the output in another file as FILE2 as:-

'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J',
'K', 'L'

Note:- New line should start after every 5 characters and not coma after last character.

3
xargs printf "'%s', '%s', '%s', '%s', '%s',\n" <in >out

...though that does do the comma after the last character. well, it actually does up to four trailing null fields plus the last comma...

sed  -e:n -e'$!N;s/\n/&/4;to' -e$\!bn <in >out \
     -e:o -e"s//', '/g;s/.*/'&',/;$"s/,$//

^ that doesn't.

  • I didn't knew that we could put rederections in the middle of a command!!!(+1) (I think in this case it does not help readability) – JJoao Dec 13 '15 at 12:51
  • 1
    @JJoao - sure - put em where ever you want. there's some weird stuff you can do with << heredoc_delimiters where trailing spaces and aliases are concerned. I don't know what you mean by readability - you read it. I always make an effort to do things a little differently because it's important people understand what can work and why. – mikeserv Dec 13 '15 at 15:21
0
perl -p0e 's/.+/"$&",/g; s/,\n$//; s/\n/++$i%5?" ":"\n"/ge'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.