Count delimiter and replace

Hi,
I have below sample data file. I want to count the delimiter no of times repeated and replace with new line characters. The new line characters appear somtimes in between of the records and end of the record:

input file:

jack;1500;manager;boston\n
jim;2000;\n
developer;texas\n
bob;5000;director\n
;atlanta\n

Expected output file:

jack;1500;manager;boston\n
jim;2000;developer;texas\n
bob;5000;director;atlanta\n

I am new to shell scripting. How can I achieve this. The parameter of script should be delimeter, file name and count repetition.

Thanks in advance.

-subha

I'm sure there is a better way to do this but here is my quick solution.

delimit=$1
file=$2
count=$3
sed "s/${delimit}\$//; s/^${delimit}//" $file | awk -F${delimit} -vT=$count '
{ 
    RS=""
    for(i=1; i <= NF; i++ ) {
        printf $i
        printf i % T ? FS : "\n"
    }
}'

Called like this';

$ ./solution ";" inputfile 4
jack;1500;manager;boston
jim;2000;developer;texas
bob;5000;director;atlanta

I am hitting with following error msg when I am processing files that has 40k lines.

Error msg:
^ ran out for this one

Is there any way we can fix the above error msg. I want to generate output file and turn off the screen output when I run the script.

echo -e "$(tr -d '\n' <input | sed 's:;\\n:;:g;s:\\n;:;:g;s:\\n$::')"
[ctsgnb@shell ~]$ cat input
jack;1500;manager;boston\n
jim;2000;\n
developer;texas\n
bob;5000;director\n
;atlanta\n
[ctsgnb@shell ~]$ echo -e "$(tr -d '\n' <input | sed 's:;\\n:;:g;s:\\n;:;:g;s:\\n$::')"
jack;1500;manager;boston
jim;2000;developer;texas
bob;5000;director;atlanta
[ctsgnb@shell ~]$
awk -F \; '{for (i=1;i<=NF;i++) {if ($i!="") printf (++j%4)?$i FS:$i RS}}' infile
1 Like

Nice solution rdcwayx - I know there had to be a better way to do it. Only thing to look out for is blank fields (not sure if they are allowed or not).

Tr this,

awk  -F";" '{if (NF == 4) {print $0} else {printf substr($0,1,(length($0)-2));getline;print $0}}' inputfile
local $/;
my $str=<DATA>;
$str=~s/(?<=;)\n//;
$str=~s/\n(?=;)//;
print $str;
__DATA__
jack;1500;maager;bosto
jim;2000;
developer;texas
bob;5000;director
;atlata