Insert New Line Character

Hi,
If my first character of a line starts with 2 then after 5th charecter newline character should be inserted.

Input.txt:

a1234567890
2222300007
bsdfsdf888999999
ssdfkjskfdjskfdjd
2899900000000099999999999999
28887777
999999999999999999

Output.txt:

a1234567890
22223
00007
bsdfsdf888999999
ssdfkjskfdjskfdjd
28999
00000000099999999999999
28887
777
999999999999999999

I've tried the below but not getting desired output, could you please help me in this.
Thanks in advance

awk 'BEGIN{ FS=""; ch=5}
{
 z=substr($0,0,1) ;
if  (z== 2)
    print substr($0,1,ch) "\n" substr($0,ch)
}' Input.txt>Output.txt

Regards,
Ulf

---------- Post updated at 11:57 AM ---------- Previous update was at 11:23 AM ----------

I got the output...... Its working perfectly

awk 'BEGIN{ FS=""; ch=5}
{
    if (substr($0,0,1) == 2)
    print substr($0,1,ch) "\n" substr($0,ch)
    else
    print $0
}' Input.txt>Output.txt

Are you sure that is doing what you want?

It looks like there might be an off by 1 error. I would have thought you would want to change the line:

    print substr($0,1,ch) "\n" substr($0,ch)

to:

    print substr($0,1,ch) "\n" substr($0,ch+1)

to get the output you said you wanted.

Try also (untested):

awk '{sub /^2..../, "&\n")}1' file
2 Likes

Hello,

One more approach with awk .

awk '/^2/ {print (substr($0,1,5)"\n"substr($0,6))} !/^2/ { print $0 }' file_name

Output will be as follows.

a1234567890
22223
00007
bsdfsdf888999999
ssdfkjskfdjskfdjd
28999
00000000099999999999999
28887
777
999999999999999999

Thanks,
R. Singh

That is interesting, but the lines marked in red above do no match what was requested. The request was to replace lines starting with a "2" with two lines where the 1st replacement line contains the 1st five characters of that line and the 2nd replacement line contains the rest of that line.

Your code adds two lines instead of replacing one line with two, and the 1st added line only contains the 1st four characters from that line instead of the 1st five characters.

RavinderSingh13 :

$ awk '/^2/{print (substr($0,1,4)"\n"substr($0,5)) }1' file

Duplicate lines were getting printed as noticed by Don Cragun because of both print and 1 , whenever pattern is found you should not use 1 at the end along with print in beginning, you need to use some variable something like this, to avoid line getting printed.

$ awk '{ f = /^2/ ?  0 : 1 }!f{print substr($0,1,5) RS substr($0,6)}f' file

OR simply like this

$ awk '/^2/{$0 = substr($0,1,5) RS substr($0,6)}1' file

Resulting

a1234567890
22223
00007
bsdfsdf888999999
ssdfkjskfdjskfdjd
28999
00000000099999999999999
28887
777
999999999999999999

below code should be good I have put 1 so it printed the lines 2 times.

awk '/^2/ {print (substr($0,1,5)"\n"substr($0,6))} !/^2/ { print $0 }' file_name

Output will be as follows.

a1234567890
2222
300007
bsdfsdf888999999
ssdfkjskfdjskfdjd
2899
900000000099999999999999
2888
7777
999999999999999999

Thanks,
R. Singh

Did you read below one ???

Thank you Akshay, I have edited the post now.