Splitting the line in multiple lines

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 1
Reputation: dd_sh is an unknown quantity at this point 
Solved Threads: 0
dd_sh dd_sh is offline Offline
Newbie Poster

Splitting the line in multiple lines

 
0
  #1
Mar 30th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 25
Reputation: blater is an unknown quantity at this point 
Solved Threads: 7
blater's Avatar
blater blater is offline Offline
Light Poster

Re: Splitting the line in multiple lines

 
0
  #2
Mar 31st, 2008
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
Shell Scripting Syntax (Toggle Plain Text)
  1. if( letter == "," || letter == " ") {
  2. j=m;
  3. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC