Need to split record

Hi All,
Need help in writing a shell script for the below requirement:

i/p:

123456789

o/p:

123
456
789

Req: one record should be split into multiple based on the length ( after every third character it should be moved into next line)

Thanks in Advance

Hi,
try:
bash - How to insert a new line character after a fixed number of characters in a file - Stack Overflow

Are all record the same?

o/p: 4 (+ space =5)
123 3
456 3
789 3

So divide on 3 gives wrong output

awk '{gsub(/.../,"&\n")}1'
i/p
: 1
234
567
89
awk 'gsub(/.../,"&\n",$2) {print "o/p:";print $2}'

This will give you the output required. It's only available in this case.

Or this

 awk 'BEGIN {print "o/p:"} gsub(/.../,"&\n",$2) {print $2}'
echo 123456789 |sed 's/\(...\)/\1\n/g'

@rdcwayx
Input is different

echo "i/p: 123456789" |sed 's/\(...\)/\1\n/g'
i/p
: 1
234
567
89

you may misunderstand the original request. :eek::slight_smile:

$ echo "breakAfterThreeChars:" | perl -ne  's/(\w{3,3})/\1\n/g;print'
bre
akA
fte
rTh
ree
Cha
rs:
1 Like

For me, this i/p: 123456789 is one record, and two fields.

Hi.

$ echo 123456789 | fold -w 3
123
456
789

Best wishes ... cheers, drl

2 Likes

HemaV,
Check this out.. using awk:

$ awk '{a=length($0);for(i=1;i<=a;i=i+3) print substr($0,i,3)}' file
123
456
789

Enjoy..

I believe that the point he was trying to make (and what everyone else seems to have assumed) is that i/p: and o/p: are not part of the actual data, but only labels in the forum post's text to refer to the input data and the output data.

Regards,
Alister

Aha, I did not think of that.

When the original post does not specify a platform, it would be thoughtful to avoid using proprietary extensions that provide zero benefit and have a portable counterpart.

Aside from the potentially problematic \n, the explicit capture and backreference aren't needed.

sed 's/.../&\
/g'

Note that both variations of this approach will insert additional blank lines after each line that is a multiple of 3 characters long.

In my opinion, however, anything other than fold is just an academic exercise.

Regards,
Alister

:):):):):slight_smile: