Sponsored Content
Top Forums Shell Programming and Scripting Delimted to padded conversion with unknown field length Post 302441460 by Christoph Spohr on Saturday 31st of July 2010 02:09:14 AM
Old 07-31-2010
Not pretty, but it works. It uses NF to check if the number
of fields is greater than 1.

Code:
awk -F, 'NF > 1 && NR==FNR{for (i=1;i<=NF;i++)
    {t=length($i)+2;(t>a[i])?a[i]=t:a[i]=a[i]}}
    NR!=FNR{if (NF>1) {for (i=1;i<=NF;i++)
    {printf "%-"a[i]"s", $i};printf "\n"}
    else print $0}' file file

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with finding length of a field

I have a pipe delimited file. I need to check that the first and second fields are 5 characters long and if not i need to append 0 in front of them to make them 5 characters long. can some body let mwe know how i can find the length of the two fields and then make them 5 characters long if they... (6 Replies)
Discussion started by: dsravan
6 Replies

2. UNIX for Dummies Questions & Answers

Extract substring of unknown length from string

I have a string: hgLogOutput=" +0000 files: forum/web/hook-test.txt /forum/web/hook-test-2.txt description: test" and I want to extract the file names from it, they will always appear between the files: and the description:. I have worked out that I can do this: "$hgLogOutput" | awk '{... (2 Replies)
Discussion started by: klogger
2 Replies

3. UNIX for Dummies Questions & Answers

Conversion problem with date field and formatting.

Hi, My input file contains the record(s) as below with space as FS. 01-01024180 35000 MV010 02/03/09 0306 03060226 03 02-00410330 470000 MV010 02/03/09 0301 03010276 03 1. I need to convert the field 02/03/09 (dd/mm/yy) to yyyymmdd yet retain the Field separator. Using the modified... (2 Replies)
Discussion started by: talk2pawee
2 Replies

4. Shell Programming and Scripting

timestamp field was padded with blanks warning

There are two columns on the data base table, create and update timestamps of datatype timestamp. I dont have these fields on the csv file. So I am doing the below. awk -F , -v d="$(date "+ %Y-%m-%d-%H.%M.%S")" '{ OFS=FS; print $1, $2, $3, $4, d, $5, $6, d }' temp.csv > temp1.csv and then... (3 Replies)
Discussion started by: mitr
3 Replies

5. Shell Programming and Scripting

Adding a field to a file using a conversion table

Hello everyone, Here is what i am trying to accomplish. I have a transaction log that I want to to add a field. The fields in the transaction log are tab delimited FYI. My goal is to add a column specifying the category/type to each item purchased. I have created a two column "conversion table"... (2 Replies)
Discussion started by: SpencerClark
2 Replies

6. Shell Programming and Scripting

Removing characters from end of line (length unknown)

Hi I have a file which contains wrong XML, There are some garbage characters at the end of line that I want to get rid of. Example: <request type="product" ><attributes><pair><name>q</name><value><!]></value></pair><pair><name>start</name><value>1</value></pair></attributes></request>�J ... (7 Replies)
Discussion started by: dirtyd0ggy
7 Replies

7. 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

8. Shell Programming and Scripting

Replace a field with a character as per the field length

Hi all, I have a requirement to replace a field with a character as per the length of the field. Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (*). like ******************** As the field is not a fixed one, i want to do the... (2 Replies)
Discussion started by: gani_85
2 Replies

9. Shell Programming and Scripting

Fixed length to delimited file conversion

Hi All, I need to convert a fixed length file to a delimited file with , (comma). But not all columns, some of the columns in the fixed files are used as fillers and I do not need that in the output file. test_fixed_len.txt I 0515 MR 394 I 0618 MR & MRS 942 I 0618 MR & MRS... (7 Replies)
Discussion started by: member2014
7 Replies

10. Programming

C: inputting string of unknown length

I realize this general issue (inputting strings of variable length in C) has been addressed in myriad locations before, but I'm interested in knowing why my specific approach is not working. (BTW I'm intentionally keeping the size increments small so that I can more easily follow what's going on.... (5 Replies)
Discussion started by: DevuanFan
5 Replies
ARRAY_PAD(3)								 1							      ARRAY_PAD(3)

array_pad - Pad array to the specified length with a value

SYNOPSIS
array array_pad (array $array, int $size, mixed $value) DESCRIPTION
array_pad(3) returns a copy of the $array padded to size specified by $size with value $value. If $size is positive then the array is padded on the right, if it's negative then on the left. If the absolute value of $size is less than or equal to the length of the $array then no padding takes place. It is possible to add at most 1048576 elements at a time. PARAMETERS
o $array - Initial array of values to pad. o $size - New size of the array. o $value - Value to pad if $array is less than $size. RETURN VALUES
Returns a copy of the $array padded to size specified by $size with value $value. If $size is positive then the array is padded on the right, if it's negative then on the left. If the absolute value of $size is less than or equal to the length of the $array then no padding takes place. EXAMPLES
Example #1 array_pad(3) example <?php $input = array(12, 10, 9); $result = array_pad($input, 5, 0); // result is array(12, 10, 9, 0, 0) $result = array_pad($input, -7, -1); // result is array(-1, -1, -1, -1, 12, 10, 9) $result = array_pad($input, 2, "noop"); // not padded ?> SEE ALSO
array_fill(3), range(3). PHP Documentation Group ARRAY_PAD(3)
All times are GMT -4. The time now is 02:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy