Filling out a text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filling out a text
# 1  
Old 12-05-2003
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  
Old 03-18-2009
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  
Old 03-19-2009
Quote:
Originally Posted by aaaaargh
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  
Old 03-19-2009
if he needs only want it as 4 field then it will help
Code:
awk '{printf "%04d\n" ,$0}' filename

# 5  
Old 03-19-2009
Quote:
Originally Posted by peterk
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  
Old 03-19-2009
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  
Old 03-19-2009
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'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Merging two text files by a column and filling in the missing values

Hi, I have to text files that I want to merge by the first column. The values in the first column pretty much match for the first part. However there are some values that are present in column 1 and not present in column 2 or vice versa. For such values I would like to substitute X for the... (9 Replies)
Discussion started by: evelibertine
9 Replies

2. Shell Programming and Scripting

Filling file with 10 zeros

I am stuck with a problem. I have some 100 files with extension .txt. The files look like this: 0 3 0 0 4 All files have variable number of characters and some files are completely EMPTY. I want to make columns of length 10 by putting zeros (0) after the numbers end and also 10... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

3. Shell Programming and Scripting

/tmp filling up

Does anyone know of a way to redirect the ksh default of processing data in /tmp to another file system or / something else? My ksh script is parsing large DB files and it keeps filling up /tmp on the root disk. I have a 1 Tb disk with most of its space. How do I re-direct the /tmp ksh... (6 Replies)
Discussion started by: cchelten
6 Replies

4. Solaris

Root partition filling up

I have a T1000 Sparc server that has a relatively small root partition which is 24Gb and a larger partition dedicated to /export/home that is approximately 100 Gb. We have a lot of data going to /var/audit and to /var/core/corefiles. Is there any non-destructive way to redirect files from... (4 Replies)
Discussion started by: goose25
4 Replies

5. UNIX for Dummies Questions & Answers

Need help filling in ranges

I have a list of about 200,000 lines in a text file that look like this: 1 1 120 1 80 200 1 150 270 5 50 170 5 100 220 5 300 420 The first column is an identifier, the next 2 columns are a range (always 120 value range) I'm trying fill in the values of those ranges, and remove... (4 Replies)
Discussion started by: knott76
4 Replies

6. Shell Programming and Scripting

filling a character

in my file data is like this 1,2,3 3,4,5,6,7,8 10,11,23,24 i want to make as 1,2,3,?,?,? 3,4,5,6,7,8 10,11,23,24,?,? here max no of words(separated by comma) in a line is 6.so every line contains 6 words.Line which have less than 6 words replaced with '?' as a word i have... (3 Replies)
Discussion started by: new2ubuntulinux
3 Replies

7. Emergency UNIX and Linux Support

Something is filling hard disk on its own.

I came in this morning to find that our mail server was down. Couldn't connect. I looked at logs. The logs complained about no space on device. I run df and it comfirms that the system disk (mounted on /) is at 100% capacity. I try to delete some files before attempting to look at cyrus. I rotate... (23 Replies)
Discussion started by: timgolding
23 Replies

8. UNIX for Dummies Questions & Answers

Root filesystem filling up!

Hi all. New to the forum and new to Unix admin... / filesystem filled up and I can't find where the large files are. Any help will be apppreciated: # df -k Filesystem kbytes used avail capacity Mounted on /dev/dsk/c1t0d0s0 8063580 7941745 41200 100% / /proc ... (4 Replies)
Discussion started by: jamie_collins
4 Replies

9. UNIX for Dummies Questions & Answers

filling variable with ls

hello All, Need some further help. This will make my live easier. Instead of copy and pasting I think I can automate some website building. when I do a ls from a directory I need the file names placed into a sentence. it is going about wordts like: word-AB-1234.jpg... (1 Reply)
Discussion started by: ToXiQ
1 Replies

10. Solaris

Filesystem filling up and no clue as to why!

df shows that the filesystem is filling up and the usage is 94%. However when I actually traverse to the directory I du shows only about 10% of the space occupied! Below is the output of df and du: >>>df -kh /cbmdata/00 470M 393M 29M 94% /cbmdata/00 >>>/cbmdata/00>... (3 Replies)
Discussion started by: zombiezparadize
3 Replies
Login or Register to Ask a Question