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
c program to extract text between two delimiters from some text file kukretiabhi13 High Level Programming 7 12-03-2008 06:29 PM
Building Full-Text Search Applications with Oracle Text iBot Oracle Updates (RSS) 0 04-06-2008 06:10 AM
Filling in characters to line a file up hcclnoodles Shell Programming and Scripting 1 07-27-2004 11:11 PM
grep multiple text files in folder into 1 text file? coppertone UNIX for Dummies Questions & Answers 7 08-23-2002 03:50 PM
Process responsible for filling up /var/tmp Mal UNIX for Advanced & Expert Users 3 04-25-2002 12:45 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 12-05-2003
peterk peterk is offline
Registered User
  
 

Join Date: Nov 2002
Posts: 5
Question Filling out a text

Hello,

I have a problem with filling out a text. I have different lenghts in a file and would like to see that all the lines becomes the same length by putting a zero in front off the line.

Please advice.

Old File:
----------
5432
233
3455
4432

New File:
-----------
5432
0233
3455
4432
  #2 (permalink)  
Old 03-18-2009
aaaaargh aaaaargh is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 20

Code:
awk 'BEGIN {m=0} {a[NR]=$0;if (m<length($0)){ m=length($0)}} END { for (x in a) { if (length(a[x])<m) {  for (i=length(a[x]);i<m;i++) { printf "0" }  } print a[x] }} ' file

  #3 (permalink)  
Old 03-19-2009
rikxik's Avatar
rikxik rikxik is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 250
Quote:
Originally Posted by aaaaargh View Post
Code:
awk 'BEGIN {m=0} {a[NR]=$0;if (m<length($0)){ m=length($0)}} END { for (x in a) { if (length(a[x])<m) {  for (i=length(a[x]);i<m;i++) { printf "0" }  } print a[x] }} ' file
No need for that for loop to push zeros in front - printf is smart enough:


Code:
$ nawk '{a[NR]=$0;m<length($0)&&m=length($0)}END{for(i in a){printf "%0"m"d\n", a[i]}}' of
0233
3455
4432
5432

  #4 (permalink)  
Old 03-19-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,407
if he needs only want it as 4 field then it will help

Code:
awk '{printf "%04d\n" ,$0}' filename

  #5 (permalink)  
Old 03-19-2009
rikxik's Avatar
rikxik rikxik is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 250
Quote:
Originally Posted by peterk View Post
I have different lenghts in a file and would like to see that all the lines becomes the same length by putting a zero in front off the line.
So we are doing max(different).
  #6 (permalink)  
Old 03-19-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,555
if you have Python

Code:
# more file
5432
233
3455
44322332423

# python -c 'data=open("file").read().split();print "\n".join([ i.zfill(max(map(len,data))) for i in data ])'
00000005432
00000000233
00000003455
44322332423

  #7 (permalink)  
Old 03-19-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,089

Code:
awk '{
_[NR]=$0
len=length($0)
if (len>n)
	n=len
}
END{
	for(i=1;i<=NR;i++)
		printf("%"n"s\n",_[i])
}' filename | sed 's/ /0/g'

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