Visit Our UNIX and Linux User Community


Append spaces the rows to make it into a required fixed length file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append spaces the rows to make it into a required fixed length file
# 1  
Old 01-03-2013
Append spaces the rows to make it into a required fixed length file

I want to make a script to read row by row and find its length. If the length is less than my required length then i hav to append spaces to that paritucular row. Each row contains special characters, spaces, etc.

For example my file contains ,
Code:
12345    abcdef
234    abcde
89012  abcdefgh

Each row should be of length 15, if any row is less than 15 then for the remaining length spaces should be appended.

Last edited by Scrutinizer; 01-03-2013 at 07:27 AM.. Reason: code tags
# 2  
Old 01-03-2013
Hey,

Welcome to The UNIX and Linux Forums,

Please use code tags for data samples and codes. code tag button look like Image

You should use printf to adjust the length of each lines.

Try something like this,

Code:
awk -v l="15" '{s="%-"l"s\n";printf(s,$0);}' input_file

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 01-03-2013
This works great; can you please explain how this works

Code:
# cat file
12345 abcdef
234 abcde
89012 abcdefgh


# awk -v l="15" '{s="%-"l"s\n";printf(s,$0);}' file > temp; cat temp > file


# awk '{ if (length($0) > max) max = length($0) }
END { print max }' file
15


Last edited by sathyaonnuix; 01-03-2013 at 05:57 AM..
# 4  
Old 01-03-2013
Quote:
Originally Posted by sathyaonnuix
Ranga, the row count is not reaching 15, can you please take a look. Pls advise if I am missing something.

Code:
# cat file
12345 abcdef
234 abcde
89012 abcdefgh


# awk -v l="15" '{s="%-"l"s\n";printf(s,$0);}' file
12345 abcdef
234 abcde
89012 abcdefgh


# awk '{ if (length($0) > max) max = length($0) }
END { print max }' file
14

Please check the output of my command and not the input file.

Code:
# awk -v l="15" '{s="%-"l"s\n";printf(s,$0);}' file|awk '{print length($0);}'
15
15
15

Cheers,
RangaSmilie

---------- Post updated at 05:29 AM ---------- Previous update was at 04:58 AM ----------

sathyaonnuix,

We can adjust the colunm/row length by using the printf functions format specifier.

Code:
printf("%-20s",line); -> This will fill spaces when the field is lesser than 20 characters.

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 5  
Old 01-03-2013
Wow thats clear, thanks Ranga
# 6  
Old 01-03-2013
I need to pass the length and file name as parameters. Please help how to change the below code as per that,

Code:
 
awk -v l="15" '{s="%-"l"s\n";printf(s,$0);}' file > temp;

# 7  
Old 01-03-2013
You don't necessarily need to use awk, can be done with shell (bash, ksh) builtin printf:
Code:
$ FILE=file
$ LENGTH=15
$ while read LINE; do printf "%-"$LENGTH"s\n" "$LINE"; done <"$FILE"
12345 abcdef   
234 abcde      
89012 abcdefgh

Remove - sign in front of $LENGTH if you want it right justified.

Previous Thread | Next Thread
Test Your Knowledge in Computers #402
Difficulty: Medium
The term 3D printing originally referred to a powder bed process employing standard and custom inkjet print heads.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Make it to fixed length

Hi Team, I have a different length records in my text file.I would like to make all the records with same length. I want to check the maximum lenth and all other records make the same length It's urgent request. Thanks in Advance (2 Replies)
Discussion started by: Anthuvan
2 Replies

2. Shell Programming and Scripting

How to add extra spaces to make all lines the same length?

Hello to all, I'm trying to format a file to have all lines with the same length (the length of the longest line) adding needed extra spaces at the end. Currently I have the awk script below that adds one space the end of each that have a lenght lower than 35, but I don't know how to add... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

3. UNIX for Dummies Questions & Answers

Length of a fixed width file

I have a fixed width file of length 53. when is try to get the lengh of the record of that file i get 2 different answers. awk '{print length;exit}' <File_name> The above code gives me length 50. wc -L <File_name> The above code gives me length 53. Please clarify on... (2 Replies)
Discussion started by: Amrutha24
2 Replies

4. Shell Programming and Scripting

Remove characters from fixed length file

Hello I've question on the requirement I am working on. We are getting a fixed length file with "33" characters long. We are processing that file loading into DB. Now some times we are getting a file with "35" characters long. In this case I have to remove two characters (in 22,23... (14 Replies)
Discussion started by: manasvi24
14 Replies

5. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

6. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

7. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

8. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

9. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies

Featured Tech Videos