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
seeking help in text processing Alecs Shell Programming and Scripting 8 04-22-2008 02:17 PM
Simple Event Processing != Complex Event Processing iBot Complex Event Processing RSS News 0 12-16-2007 12:10 PM
text file processing alias47 UNIX for Dummies Questions & Answers 1 08-09-2007 12:10 PM
Processing a text file TheCrunge UNIX for Dummies Questions & Answers 1 11-09-2005 11:47 AM
grep multiple text files in folder into 1 text file? coppertone UNIX for Dummies Questions & Answers 7 08-23-2002 03:50 PM

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 02-29-2008
anchal_khare anchal_khare is offline
Registered User
  
 

Join Date: Jun 2007
Location: Mumbai,India
Posts: 325
text processing ( sed/awk)

hi..
I have a file having record on in 1 line....

I want every 400 characters in a new line...

means in 1st line 1-400
in 2nd line - 401-800 etc


pl help.
  #2 (permalink)  
Old 02-29-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by anchal_khare View Post
hi..
I have a file having record on in 1 line....

I want every 400 characters in a new line...

means in 1st line 1-400
in 2nd line - 401-800 etc


pl help.

Code:
sed -e "s/.\{400\}/&\n/g" file.txt

  #3 (permalink)  
Old 02-29-2008
anchal_khare anchal_khare is offline
Registered User
  
 

Join Date: Jun 2007
Location: Mumbai,India
Posts: 325
hi vino...
its not working..
its showing me the same output...
i tested in a small file having abcdefghijklmopqrstuvwxyz in a line....

so after -e "s/.\{4\}/&\n/g" file.txt

the output should be
abcd
efgh
ijkl
lmno
...
...


but its unchaged..

pl help
  #4 (permalink)  
Old 02-29-2008
krishmaths krishmaths is offline
Registered User
  
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 191
script


total_chars=`wc -c filename | cut -c 1-8 `
block_size=400
cut_from=1
cut_to=400
total_chars=`expr $total_chars + 0`
#The above step is to get total_chars treated as number for below comparison
while [ $cut_from -le $total_chars ]
do
cut -c `echo $cut_from`-`echo $cut_to` filename >> outfile
cut_from=`expr $cut_from + $block_size`
cut_to=`expr $cut_to + $block_size`
done
  #5 (permalink)  
Old 02-29-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,342
A solution with awk:


Code:
awk 'BEGIN{nr=1}{while(substr($0,nr,400)){print substr($0,nr,400);nr+=400}}' file

Regards
  #6 (permalink)  
Old 02-29-2008
krao krao is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 16
Thanks Franklin52,

hi anchal_khare,

u can also check the below code

awk '{n=1;while( n<=length($0)) { t = substr($0,n,4); print t"\n"; n= n+4} }' Filename

Input:
abcdefghijklmnopqrstuvwxyz

output
abcd

efgh

ijkl

mnop

qrst

uvwx

yz
Closed Thread

Bookmarks

Tags
linux, solaris

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 11:27 PM.


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