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

awk doesn't have "then" and "fi" like shell does. It uses a c/java like syntax where || means "or", && means "and", "==" means equals.

e.g

if( letter == "," || letter == " ") {
     j=m;
  }
This article has been dead for over six months. Start a new discussion instead.