Left pad spaces using awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Left pad spaces using awk or sed
# 1  
Old 12-10-2012
Left pad spaces using awk or sed

Hi,I've a unix pipe delimited file as below
Code:
f1|f2|f3|f4|f5|f6

My requirement is to pad spaces on the left to fields f2, f3 and f5. Field Lengths according to file layout f2 - 4 char f3 - 5 char f5 - 3 char If my record is as below
Code:
1|43|bc|h0|34|a

Output record should be as below
Code:
1|  43|   bc|h0| 34|a

Kindly provide a solution using awk or sed. Appreciate help! Regards,Soujanya

Last edited by Scrutinizer; 12-10-2012 at 12:34 PM.. Reason: code tags
# 2  
Old 12-10-2012
Code:
awk -F"|" '{printf "%d|%4d|%5d|%d|%3d|%d", $1,$2,$3,$4,$5,$6}' file

# 3  
Old 12-10-2012
Hi,Thanks for the above solution. Since the file has string type data used %s instead of %d.
Code:
awk -F"|" '{printf "%s|%4s|%5s|%s|%3s|%s", $1,$2,$3,$4,$5,$6}' input_file

But this awk statement becomes too lengthy if my source file has around 25 fields or more. So let me know if any simple awk or sed can be used to achieve the same. Regards,Soujanya

Last edited by Scrutinizer; 12-10-2012 at 12:35 PM.. Reason: code tags
# 4  
Old 12-10-2012
try:
Code:
awk -F"|" '
BEGIN{ f[2]=4; f[3]=5; f[5]=3; }
{for (i=1; i<=NF; i++) printf("%*s%s", f[i], $i, i==NF?"\n":FS);}
' input

set column widths on begin line as needed.
This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 12-10-2012
Code:
awk '{$2=sprintf("%4s",$2); $3=sprintf("%5s",$3); $5=sprintf("%3s",$5)}1' FS=\| OFS=\| file


Last edited by Scrutinizer; 12-10-2012 at 12:54 PM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk pad 1 column with leading zero if char > 12

Hello, I got a question. I have several csv files with lots of data in it and for the first column i have EAN codes. The problem that i am facing is that some of these codes have the leading 0 removed so they are 12 or less chars while a EAN code is (always?) 13 chars. For this i used a... (9 Replies)
Discussion started by: SDohmen
9 Replies

2. Shell Programming and Scripting

left join using awk

Hi guys, I need AWK to merge the following 2 files: file1 1 a 1 1 2 b 2 2 3 c 3 3 4 d 4 4 file2 a a/a c/c a/c c/c a/a c/t c c/t c/c a/t g/g c/c c/t desired output: 1 a 1 1 a/a c/c a/c c/c a/a c/t 2 b 2 2 x x x x x x 3 c 3 3 c/t c/c a/t g/g c/c c/t 4 d 4 4 x x x x x x (2 Replies)
Discussion started by: g1org1o
2 Replies

3. Shell Programming and Scripting

left join using awk

Hi guys, I need to use awk to join 2 files file_1 A 001 B 002 C 003 file_2 A XX1 B XX2 output desired A 001 XX1 B 002 missing C 003 XX2 thank you! (2 Replies)
Discussion started by: g1org1o
2 Replies

4. Shell Programming and Scripting

Unix Cut or Awk from 'Right TO Left'

Hello, I want to get the User Name details of a user from a file list. This list can be in the format: FirstName_MiddleName1_LastName_ID FirstName_LastName_ID FirstName_MiddleName1_MiddleName2_LastName_ID What i want it to return is FirstName_MiddleName1_LastName of a user. I... (6 Replies)
Discussion started by: limamichelle
6 Replies

5. Shell Programming and Scripting

How to delete ending/trailing spaces using awk,sed,perl?

How to delete ending/trailing spaces using awk,sed,perl? Input:(each line has extra spaces at the end) 3456 565 3 7 35 878 Expected output: 3456 565 3 7 35 878 (5 Replies)
Discussion started by: cola
5 Replies

6. Shell Programming and Scripting

How to remove spaces using awk,sed,perl?

Input: 3456 565 656 878 235 8 4 8787 3 7 35 878 Expected output: 3456 565 656 878 235 8 4 8787 3 7 35 878 How can i do this with awk,sed and perl? (10 Replies)
Discussion started by: cola
10 Replies

7. Linux

sed couldn't flush stdout no space left on device

I am running Oracle Linux enterprise server 5.0. I just installed JDE 9.0 and after I started Webserver my root directory is 100% full. Can some one help me flush stdout. I am new to linux. Sam (5 Replies)
Discussion started by: s1a2m3
5 Replies

8. Shell Programming and Scripting

Fixed Width Join & Pad Sed/Awk Help

I was wondering someone might be able to push me in the right direction, I am writing a script to modify fixed-width spool files, As you can see below the original spool file broke a single line into two for printability sake. I have had been able do the joins using sed, the thing I am... (10 Replies)
Discussion started by: Cho Nagurai
10 Replies

9. Shell Programming and Scripting

Left join on files using awk

nawk 'NR==FNR{a;next} {if($1 in a) print $1,"Found" else print}' OFS="," File_B File_A The above code is not working help is appreciated (6 Replies)
Discussion started by: pinnacle
6 Replies

10. Shell Programming and Scripting

How to pad spaces

Hello, I have to write a function to input a Label and a number, and output a line as the following format: Column 1 to 30: field label, left justified. Column 31 to 45: A number, right justified. The middle is padded with space. May I know how can I achieve this? (I don't know how to count... (3 Replies)
Discussion started by: sarahho
3 Replies
Login or Register to Ask a Question