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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
make multiple line containing a pattern into single line VTAWKVT Shell Programming and Scripting 10 4 Weeks Ago 06:54 AM
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-27-2008 11:43 PM
Splitting the line in multiple lines dd_sh Shell Programming and Scripting 3 03-31-2008 10:54 AM
Reading multiple lines as single braindrain Shell Programming and Scripting 9 10-18-2006 07:16 AM
Need output in different lines not in one single line csaha Shell Programming and Scripting 1 02-08-2006 04:28 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-22-2005
Registered User
 

Join Date: Mar 2004
Posts: 24
Splitting a single line into multiple lines

I have a case where, I need to look into a file.
Go to each line of the file, find the length of the line, if the length of the line is more than 75 chars, I need to split the line into multiple lines of 75chars max. If the length of the line is less than 75, we need not do anything.
So at the end of this operation , maximum length of anyline in the file should be only 75.

Ex: file having 3 lines
1st line: 20chars
2nd line: 85chars
3rd line: 160chars

after the executing the script,
The file should be

1st line:20 chars

2nd line: 75 chars
3rd line: 10 chars

4th line: 75 chars
5th line: 75 chars
6th line: 10 chars

Thanks for your help
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-22-2005
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
Try the following ....

But the code is tested with 5 chars as criterion instead of 75.
Change as per your requirement.


Code:
awk '{
  if ( length($0) > 5 )
  {
      str=$0 ;
      i=0 ;
      while(length(str) > 5)
      {
        printf("%s\n",substr($0,i+1,5) );
        i+=5 ;
        str=substr($0,i,length($0) );
      }

      if( length(str) > 1 )
        printf("%s\n", substr(str,2,length(str))) ;

  }
  else
  {
      print $0
  }
}' file1
Reply With Quote
  #3 (permalink)  
Old 02-22-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,226
Or...
Code:
fold -75 file1 > file2
Reply With Quote
  #4 (permalink)  
Old 02-22-2005
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
fold -w 5 file1 > file2


good one Ygor ! I prefer this one compared to the long one i wrote.:-)
Reply With Quote
  #5 (permalink)  
Old 02-23-2005
Registered User
 

Join Date: Mar 2004
Posts: 24
Thanks for your responses. It's working great...
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0