Padding in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Padding in Unix
# 1  
Old 01-22-2008
Padding in Unix

I have a file with different character counts on each line
how do i make it with unique character counts.

example:

1st line : ABCD 011 XYZ 0000 YYYY BBB TEADINGDA
2nd line: ABCD 011 xys 0010 YYYY BBB TEAD
3rd line : ABCD 022 YXU 000 UUU BBB TE

1st line is 43 characters
2nd line is 39 characters
3rd line is 37 characters

how do i make all line to be 41 characters

Thanks
# 2  
Old 01-22-2008
I think we need more information.
For example, you could pad the lines with spaces at the end or the front.
Another choice is to expand the column width for each column to some max size, padding each column to that size.

What result do you need?
This pads spaces at the end:
Code:
awk '{ printf("%-41s", $0) }' filename > tmpfile
mv tmpfile filename

# 3  
Old 01-22-2008
Quote:
Originally Posted by rudoraj
I have a file with different character counts on each line
how do i make it with unique character counts.

example:

1st line : ABCD 011 XYZ 0000 YYYY BBB TEADINGDA
2nd line: ABCD 011 xys 0010 YYYY BBB TEAD
3rd line : ABCD 022 YXU 000 UUU BBB TE

1st line is 43 characters
2nd line is 39 characters
3rd line is 37 characters

how do i make all line to be 41 characters

Thanks
Code:
fmt -w 41 file

# 4  
Old 02-21-2008
Above two suggestions are good and solve the question specifically, but if you need a variation with your end-of-line characters (like a zero or other strings), then this is conceptually simple and modifiable

cat filename | sed -e 's/$/random chars spaces numbers etc/' | cut -c1-41

Replace "random chars spaces numbers etc" with spaces/phrases/etc
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Left padding in Unix

I am passing input string,length, and the pad character. input string=123 Pad char=# Length=6 then the output should be: ###123 How we can do this? Thanks (5 Replies)
Discussion started by: pandeesh
5 Replies

2. Shell Programming and Scripting

bash padding

Hi all Is there a way to pad the output of a bash script see that code below for i in `sed -n '/Start Printer/,/End Printer/p' /u/ab/scripts/hosts.conf | awk '!/^#/ {print $2}' | egrep -v 'broke|primera' `; do pages=`snmpget -Ov -v1 -c public $i sysLocation.0 | awk '{print $2}'` ... (3 Replies)
Discussion started by: ab52
3 Replies

3. Shell Programming and Scripting

Padding leading zero

hi All i am new to linux... source txt .. 281-BUM-5M BUM-5M 0 0 282-BUM-5M BUM-5M 0 0 83-BUM-5M BUM-5M 0 0 is it possible to use bash script to convert to (remove the "-" and fill up to 4 digit" ? 0281 BUM-5M BUM-5M 0 0 0282 BUM-5M BUM-5M 0 0 0083 BUM-5M BUM-5M 0 0 thanks a ... (5 Replies)
Discussion started by: samoptimus
5 Replies

4. Shell Programming and Scripting

Padding with zeros.

Hi Friends, I would like to left pad with "0's" on first column say (width six) I have a large file with the format: FILE: 1: ALFRED 84378 NY 8385: JAMES 88385 FL 323: SMITH 00850 TX My output needs to be like: 000001: ALFRED 84378 NY 008385: JAMES 88385 FL 000323: SMITH... (10 Replies)
Discussion started by: sbasetty
10 Replies

5. UNIX for Dummies Questions & Answers

Zero padding dates

I have a file with records containing dates like: SMPBR|DUP-DO NOT USE|NEW YORK||16105|BA5270715|2007-6-6|MWERNER|109||||JOHN||SMITH|MD|72211118||||||74559|21 WILMINGTON RD||D|2003-11-6|SL# MD CONTACT-LIZ RICHARDS|||0|Y|N||1411458| How can I get the date fields in each of my records to be... (1 Reply)
Discussion started by: ChicagoBlues
1 Replies

6. Programming

Byte Padding

Hi, Can someone explain what is byte padding? For ex: struct emp{ char s; int b; char s1; int b1; long b3; char s3; } What will be the size of this structure? Thanks (6 Replies)
Discussion started by: naan
6 Replies

7. Programming

Padding variables

Is there a function in c that will allow me to pad variables? I have an int that can't be longer than 10. I need to pad a numeric value with leading zeros 314 0000000314 (1 Reply)
Discussion started by: flounder
1 Replies

8. UNIX for Dummies Questions & Answers

Padding

Hi Can anyone tell me how to pad zeroes on the left side to a numeric string in unix shell scripting Your answer is very much appreciated Thanks Vijay (2 Replies)
Discussion started by: vijaygopalsk
2 Replies

9. Programming

Zero Padding to a string

I am writing a C program which a part of it needs to padding zero in front of a string. The program will get a sting from an ASCII file which the maxium length of this string is 5 char long. The string can sometimes less the 5 char long. In order to make it with the same length '0's are being... (3 Replies)
Discussion started by: Wing m. Cheng
3 Replies

10. UNIX for Advanced & Expert Users

Padding issues

Hello, Structure padding & structure size are different on Compaq & HP UNIX. When structures are transfered via netfork from Compaq to HP will this be a problem? If yes, what can be the solution? Thanks, shilpa (2 Replies)
Discussion started by: informshilpa
2 Replies
Login or Register to Ask a Question