| | |
Splitting the line in multiple lines
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
Solved Threads: 0
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
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
e.g
Shell Scripting Syntax (Toggle Plain Text)
if( letter == "," || letter == " ") { j=m; }
![]() |
Similar Threads
- how to format the form textarea? (PHP)
- Evolution from 3G to 4G and beyond (5G) (Computer Science)
Other Threads in the Shell Scripting Forum
- Previous Thread: sed problem
- Next Thread: Help needed in converting natural numbers to bit
| Thread Tools | Search this Thread |





