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



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
make multiple line containing a pattern into single line VTAWKVT Shell Programming and Scripting 13 12-04-2008 06:40 PM
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 02:43 AM
Splitting the line in multiple lines dd_sh Shell Programming and Scripting 3 03-31-2008 01:54 PM
Reading multiple lines as single braindrain Shell Programming and Scripting 9 10-18-2006 10:16 AM
Need output in different lines not in one single line csaha Shell Programming and Scripting 1 02-08-2006 08:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-22-2005
thanuman thanuman is offline
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
  #2 (permalink)  
Old 02-23-2005
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
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
  #3 (permalink)  
Old 02-23-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Or...
Code:
fold -75 file1 > file2
  #4 (permalink)  
Old 02-23-2005
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
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.:-)
  #5 (permalink)  
Old 02-23-2005
thanuman thanuman is offline
Registered User
  
 

Join Date: Mar 2004
Posts: 24
Thanks for your responses. It's working great...
Sponsored Links
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 07:04 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