To flat file, append null or space if its length is less than 10


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers To flat file, append null or space if its length is less than 10
# 1  
Old 06-25-2015
To flat file, append null or space if its length is less than 10

Hi,
We receive flat files with fixed width data
Now our goal is append from right null or space to each record if the lenght of the record is less than for example 10.
for example
Code:
123 45 6 0
123 45    
123 45 6  
123

and output should be
Code:
123 45 6 0
123 45****
123 45 6**
123*******

is there any single line command or small script available?

Last edited by Scrutinizer; 06-25-2015 at 03:07 PM.. Reason: CODE tags
# 2  
Old 06-25-2015
Padding a line with space characters can be done with printf.
Code:
man printf

Is this home work / class work?
# 3  
Old 06-25-2015
Quote:
Originally Posted by shharrath
Hi,
We receive flat files with fixed width data
Now our goal is append from right null or space to each record if the lenght of the record is less than for example 10.
for example
Code:
123 45 6 0
123 45    
123 45 6  
123

and output should be
Code:
123 45 6 0
123 45****
123 45 6**
123*******

is there any single line command or small script available?
Code:
$ cat test.file 
123 45 6 0
123 45
766 20 20 20 29 
123 45 6
123

$ perl -nle 's/\s+$//; $l=length; print $_, "*" x (10 - $l)' test.file
123 45 6 0
123 45****
766 20 20 20 29
123 45 6**
123*******


Last edited by Aia; 06-25-2015 at 03:24 PM..
# 4  
Old 06-25-2015
try also:
Code:
pad_char="*" ; line_width=10 ; awk '{$(w+1)=""; print substr($0, 1, w)}' FS="" OFS="$pad_char" w=$line_width infile


Last edited by rdrtx1; 06-25-2015 at 03:54 PM..
# 5  
Old 06-25-2015
I think the OP meant to use * to represent a single space.
# 6  
Old 06-25-2015
Padding spaces is effectively done with printf.
In shell one needs an explicit loop:
Code:
while read -r line
do
  printf "%-10s\n" "$line"
done < file > newfile

awk has a built-in loop.
Code:
awk '{printf "%-10s\n", $0}' file > newfile

perl does a loop with the -n option.
Code:
perl -nle 'printf "%-10s\n", $_' file > newfile

and additionally has the -i option to copy the output back to the input file.
Code:
perl -nle 'printf "%-10s\n", $_' -i file

# 7  
Old 06-26-2015
append nulls to records

thanks all for your replysSmilie

Code:
awk '{printf "%-10s\n", $0}' file > newfile

for me this small code suits best & may i also ask you to give brief sentences where exactly nulls or spaces are padding from that code

Last edited by Scrutinizer; 06-26-2015 at 04:27 AM.. Reason: CODE tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove first NULL Character in Flat File

We have a flat file with below data : ^@^@^@^@00000305^@^@^@^@^@^@430^@430^@^@^@^@^@^@^@^@^@09079989530As we can see ^@ is Null character in this file I want to remove only the first few null characters before string 00000305 How can we do that, any idea. I want a new file without first few... (5 Replies)
Discussion started by: simpltyansh
5 Replies

2. Shell Programming and Scripting

Append 0's based on length

I'm having data like this, "8955719","186497034","0001","M","3" "8955719","186497034","0002","M","10" "8955719","186497034","0003","M","10" "8955719","186497034","0004","M","3" "8955723","186499034","0001","M","3" "8955723","186499034","0002","M","10" "8955723","186499034","0003","M","10"... (3 Replies)
Discussion started by: Artlk
3 Replies

3. Shell Programming and Scripting

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 , 12345 abcdef 234 abcde 89012 abcdefgh ... (10 Replies)
Discussion started by: Amrutha24
10 Replies

4. Shell Programming and Scripting

Need a ksh script for adding the space at the end of record in a flat file

Hi, I need a ksh script for the below requirement: i have a Delimited flat file with 200 records delimiter is '|~|' i need a script to insert space at the end if the record is ending with delimiter '|~|' if it didnt end with delimiter it should not append space. Example: ram|~|2|~| ... (16 Replies)
Discussion started by: srikanth_sagi
16 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

Fixed length flat file extraction

Hii ,I am new to Unix ,i have a flat file which is (fixed length) sitting in unix,Which is holding the data for a table.I want to extract one column(length7-10) on the basis of another column(length13-15) and want only one single row Example: Below is the sample of flat file. 1111 AAAA 100 ... (4 Replies)
Discussion started by: laxmi1166
4 Replies

7. Shell Programming and Scripting

reading fixed length flat file and calling java code using shell scripting

I am new to shell scripting and I have to to the following I have a flat file with storename(lenth 20) , emailaddress(lenth 40), location(15). There is NO delimiters in that file. Like the following str00001.txt StoreName emailaddress location... (3 Replies)
Discussion started by: willywilly
3 Replies

8. Shell Programming and Scripting

how to read fixed length flat file....

Hi Gurus, Thanks in advance... I am new to writing shell scripting and help me out reading a flat file with fixed length. I have a fixed length flat file with storename(lenth 6) , emailaddress(lenth 15), location(10). There is NO delimiters in that file. Like the following str00001.txt... (2 Replies)
Discussion started by: willywilly
2 Replies

9. Shell Programming and Scripting

Exporting a flat fixed length file (Urgent)

Hi All, So far, I've been extracting data from db2 tables and exporting the file as a tab delimited file into a UNIX server using the following command: export to /.../.../.../.../.../SM_RAW_DATA.dat of del modified by coldel| nochardel select a.accno, a.CUR_BL_AM, ... (1 Reply)
Discussion started by: jj2485
1 Replies

10. UNIX for Dummies Questions & Answers

Conditional sorting on fixed length flat file

I have a fixed length file that need to be sorted according to the following rule IF B=1 ORDER by A,B Else ORDER by A,C Input file is ABC 131 112 122 231 212 222 Output needed ABC 112 131 122 212 231 222 (1 Reply)
Discussion started by: zsk_00
1 Replies
Login or Register to Ask a Question