Splitting the line in multiple lines

Hi Guys,

I need a help. I am pretty new to the shell level programing. I was trying to split a long lines to a multiple lines with few conditions. I goggle for the code and found some snippets and tried to modified it but I got few strange problems too.

I have to split the lines if line is longer than 120 characters, then I have to separate it by adding "\" at the end of around 120th character. Note it can only separated at delimitation positions such as space or comma. And here is the code for that

#!/bin/sh

awk '{
if ( length($0) > 120 )
{
str=$0 ;
i=0 ;

  while\(length\(str\) > 120\)
    \{
        j=0;
        for \(m=1; m<=120;m\+\+\)
        \{  
            letter=substr\($0,i\+m,1\);

#printf("%s\n",letter);
if( letter !=",")
then
else
{
j=m;
# printf("%s\n",substr($0,i+1,j) );
} fi
if( letter !=" ")
then
else
{ j=m;}
fi

     \}
       printf\("%s/\\n",substr\($0,i\+1,j\) \); 
   
    i\+=j ;
    str=substr\($0,i,length\($0\) \);
  \}

  if\( length\(str\) > 1 \)
    printf\("%s\\n", substr\(str,2,length\(str\)\)\) ;

}
else
{
print $0
}
}' myfile.txt

But my problem was that above condition is working if put in posted order. If I used to work with equal to and put the expression after then it is not working properly.

I was also not able to use the OR expression for comma and space

I also need the program which can revert the same operation. I also read few tutorial about sed but found its quite confusing for new users:confused::confused:

Old C programmer, huh? Welcome to high-level languages, where you don't have to reinvent strlen in every project.

man fold

perl -pe 's/(.{1,119}[ ,])/\1\\\n/g'

I don't understand the question about OR. What did you try, and how didn't it work?

if (letter == " " || letter == ",")

Hi
Thanks for the reply.
Yeah I have worked on C and VB but not in these shell level programming. And main problem for me on these programing is they hardly give me error message and I can't check the condition on debug mode.

For OR logic I tried

if (letter = " " -o letter = ",") 

as I found on some tutorials.
And I also read that for equals to we have to use single '=' not as '=='' like C++

Is this line do the same operation as mine???

perl -pe 's/(.{1,119}[ ,])/\1\\\n/g'

It is really difficult to find out what is happening with these code. I know few things
a) to handle the strings, we have to give ' at the start and end of the command
b) all arguments are should be separated by /
I really don't know any thing besides those from this code

:o:o

You have been reading shell programming tutorials (and possibly C shell, if they advice round parentheses in the if -- that's different from Bourne shell, sort of like Java is quite unlike JavaScript, although not quite as unrelated) but the program you have written is in the awk language, which is not at all the same thing. Compare to how you can use a DLL written in another language from a C program. But with scripts, the source can be in two different languages, and often is -- you frequently put small awk or sed or Perl or TCL or ... other scripts in shell scripts where the shell's own syntax or facilities don't match the problem space well enough.

The Perl program does what I imagine your spec says it should do. Try it and see. It replaces multiples of 119 charactes or less followed by a space or a comma with the same string plus a backslash and a newline. You could do the same in sed or awk, albeit not quite as succinctly; the main concept here is a regular expression, which is a facility all of these languages provide. (I think you have it in VB, too, and there are C libraries you can link to if you like.) Read up on that, it's an extremely useful and versatile tool, quite independent of any particular language.