The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 03-29-2008
dd_sh dd_sh is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 2
Unhappy 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