The UNIX and Linux Forums  

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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 03:43 AM
using tr to put multiple lines of output into one line otes4 Shell Programming and Scripting 3 02-18-2008 11:30 AM
Concatenating multiple lines to one line if match pattern phixsius Shell Programming and Scripting 13 01-24-2008 11:02 PM
Split a huge line into multiple 120 characters lines with sed? jerome_1664 Shell Programming and Scripting 2 08-17-2006 01:03 PM
Splitting a single line into multiple lines thanuman Shell Programming and Scripting 4 02-23-2005 04:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #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
  #2 (permalink)  
Old 03-30-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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?


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

  #3 (permalink)  
Old 03-31-2008
dd_sh dd_sh is offline
Registered User
  
 

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

Code:
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???

Code:
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

  #4 (permalink)  
Old 03-31-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0