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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add-remove characters to a field

Status
Not open for further replies.

kartapacio

Technical User
Sep 26, 2003
6
ES
Hi,

I am trying to convert a file to a particular format.
What i want is to get field $5 (for example) 32char long so, if its less than 32, add chars up to 32 and if its more than 32, cut them down...

I think it will be something like :

if (length($5) < 32) {

while (length($5) <33 {
add space to $5
}
}

But i can´t find the actual way of doing it...

Someone can give me a hand with this?

 
You may try something like this:
$5=sprintf("%32.32s",$5)

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV !!!

ummmmmm im afraid i cant get this to work... im sure im doing something wrong.

I dont know the meaning of "%32.32s", could you comment the line? (sorry for the abuse)


Also, how will be the actual awk statement if i have for example 2 fields and one has to be 9 char long and the other 32 ?


Thanks in advance
 
In the man awk pages take a look at sprintf.
Next can you please post
1) what you have so far
2) example of input
3) output expected related to the input

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

OK, i´ve got a file with several fields (its coming from an excel file) I have to integrate this with a cobol application that is expecting some file format to create its tables from such file.

I can get the excel file, remove all the fields that i dont need, order them...

The problem is that the cobol appl is expecting the fields to be of a particular length. What i have to control is that the text file i generate has its fields with the length cobol is expecting.

For example, field $1 (coming from excel column A) is a date, so no problem cause i know the format will be dd-mm-yy but next field ($2 , column B) is the "address". Cobol is expecting that field to be 32 char long. So i have to either add extra chars, or truncate the string if its over 32. The same happens with following field but this time with 25 chars(lets say, the name of the guy)

Example:

date address name
12-12-03;31 abbey road;Marian Papadopolis
13-03-04;54 hill street avenue;Jo Smith

You see? i need to convert variable length field to constants.











 
With this sample input file:
12-12-03;31 abbey road;Marian Papadopolis
13-03-04;54 hill street avenue;Jo Smith
Try something like this:
awk -F\; '{
printf "%s%-32.32s%-25.25s\n",$1,$2,$3
}' /path/to/input

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top