Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set fieldsize in a text file using SED or AWK 2

Status
Not open for further replies.

udaybo

Programmer
Jun 2, 2008
23
US
Hello Everyone,

I am pretty new to Sed and Awk, I am creating a report using Business Objects, its output file is going to be in TEXT format, and I need to format this file into a certain format again using either SED or AWK.

I have around 10 fields in the file, for example account number, name, address etc etc , I need to set field size to them i.e account number should be in first 10 columns or from 1 to 10th column, address should be from 11th to 30th column.

And my other question is leading zeros for example account number is 23456 then it should look like 0000023456.

Can I achieve the above using sed & awk, if yes then can anyone please help me out.

ex:
2087451 ADAMS,S 573 OLD POINT AVE

Thank you
 
Hi

So the first field is left padded with 0s to 10 characters, the second field is right padded with spaces to 20 characters :
Code:
awk '{printf"%010d%-20s\n",$1,$2}' /input/file

Feherke.
 
Thanks a lot for the reply,
I have run the script and it works fine for me, but I am not able to add a third field and so on

Below is what I have changed

awk '{printf"%010d%-20s-30s\n",$1,$2,$3}' /input/file

and please excuse me, I am very new to scripting

Thanks Again
 

You missed the % before -30s. [3eyes]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thank you LKbrwnDBA,

I am running the below script
awk '{printf"%010d%-8s%08s\n %59s\n",$1,$2,$3,$4}' TMC.txt

and I get the below output, but I am getting X in a different line, how can I get everything in one line.

I guess n goes for newline in script, but what should I use instead of n

I would appreciate any help

output
00000000000000000000103914
X
00000000000000000002007671
X

Thank you
 

Take out the first \n (this indicates next line or line feed).


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Hi

udaybo said:
please excuse me, I am very new to scripting
Well, the [tt]printf[/tt] is not a native scripting feature. It comes from C. See man 3 printf for the original C function's documentation, that is implemented more or less in [tt]awk[/tt], [tt]perl[/tt], [tt]bash[/tt], [tt]ksh[/tt], ...

Feherke.
 
Hello all

I have a file and I oly want lines starting with a certain character ex: 'S'

how do I do it in Awk or Sed

start xxxxxxxx
stop xxxxxxxxx
1234 xxxxxxx

start xxxxxxx

Thank you
 
awk '/^s/' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top