Avatar of dirknibleck
dirknibleck
 asked on

Unix Shell Script: Take text file and insert a new line every 94 characters

I have a text file where all of the text appears on the first line. I need to insert line after every 94 characters. How can I go about doing this in shell?
System ProgrammingLinux OS DevUnix OS

Avatar of undefined
Last Comment
woolmilkporc

8/22/2022 - Mon
woolmilkporc

If it's really just one line in the input file:

awk 'BEGIN{n=1}{while(substr($0,n,94)){print substr($0,n,94);n+=94}}' inputfile
ASKER CERTIFIED SOLUTION
tel2

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
wesly_chen

@wmp
Your awk won't work.

With test data
--- inputfile ---
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
09876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210987654321
-------------

your script output is
-------
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234
567890
dirknibleck

ASKER
woolmilkporc - I'm getting an error because awk won't accept an input line longer than 3000 bytes.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
woolmilkporc

Which is your OS? My AIX awk is working fine (Ok, I tested just up to 70K line length).

Do you have nawk ? Maybe this one would do.

wmp
dirknibleck

ASKER
tel2 - do you know how I can use your script with variables? The following assumes that the input is "ach" instead of ach...


ach=some_changing_filename.txt ; export ach
(perl -0pe 's/.{94}/$&\n/sg' ach) > ach

Open in new window

dirknibleck

ASKER
woolmilkporc - HPUX. nawk is not found...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
dirknibleck

ASKER
tel2 - nevermind, I'm an idiot. $ach.
woolmilkporc

So will will have to download and install gawk (GNU awK) for HPUX:

http://hpux.connect.org.uk/hppd/hpux/Gnu/gawk-4.0.0/

or maybe mawk for HPUX:

http://hpux.connect.org.uk/hppd/hpux/Shells/mawk-1.3.4.0625/

wmp
tel2

Hi dirknibleck,
Thanks for the points.
As indicated in the notes in my post:
- If you want to replace the input file, you could do this:
    perl -i -0pe 's/.{94}/$&\n/sg' inputfile

So if your filename is in $ach, you could just go:
    perl -0pe 's/.{94}/$&\n/sg' $ach
Works for me.
Either way, I don't think exporting is necessary, and when I run this:
    ach=some_changing_filename.txt ; export ach
    (perl -0pe 's/.{94}/$&\n/sg' $ach) > $ach
I end up with an empty file, as tends to happen with that kinda thing.

Hi wmp,
I get the same output wesly_chen got, when I ran your solution on GNU Linux with gawk 3.1.3.
As another example, if I run this:
    cal | gawk 'BEGIN{n=1}{while(substr($0,n,94)){print substr($0,n,94);n+=94}}'
I just get this single line of output:
     August 2011
What do you get on AIX, and what version of gawk are you running?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
wesly_chen

My guess the gawk in Linux, the "newline" character break the $0,
woolmilkporc

Guys,

to test my solution you should use an input line which is more than 94 characters long, don't you think so?
Or replace the "94" in my code with e.g. "5" and retry.

And I stated explicitly that this solution as posted will only work for one single line of input!

For multi-line input we will have to experiment with setting RS and FS:

awk 'BEGIN{n=1; RS=""; FS="\n"}{while(substr($0,n,94)){print substr($0,n,94);n+=94}}' inputfile

Note: the above will leave the linefeeds intact, so lines will not be joined.

wmp