11-28-2006
AWK and padding values
Hi,
I am trying to parse a file of fixed length transactions into a new file. Using awk I need to add the line number as a 5 digit long numeric value to each of the transactional rows.
i.e.
awk '{print <ROW NUM PADDED HERE>substr($0,1,8).........}' filename > newfile
I know that NR-1 gives me the row number (as the file has a header record), however if I add NR-1 to the awk command this will give me records that are varying length i.e. the transaction on row 10 will be 1 charachter longer than the records on rows 1-9, and the records on lines in excess of 100 will be longer still.
Therefore, as part of the awk statement I need to pad the row number with leading zeros until it is 5 numbers long (the row number will never exceed 99999). I have read that using typeset -Z may be the way to achieve this, however using the syntax below only one leading zero is added to the row number, i.e. 01,02,03......010,011......0100,0101 etc. again this creates rows of varying lengths.
awk '{print typeset -Z5 NR-1substr($0,1,8).........}' filename > newfile
Can anyone help me with the correct syntax?
Thanks.
8 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Hi
Can anyone tell me how to pad zeroes on the left side to a numeric string in unix shell scripting
Your answer is very much appreciated
Thanks
Vijay (2 Replies)
Discussion started by: vijaygopalsk
2 Replies
2. Shell Programming and Scripting
Dear Guyz:)
I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z).
I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies
3. Shell Programming and Scripting
I have read another post about this issue and am wondering how to adapt it
to my own, much simpler, issue.
I have a file of user IDs like so:
333333
321321
546465
...etc
I need to take each number and use it to print records wherein the 5th
field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies
4. Shell Programming and Scripting
Hello, i am trying to pad a column
A1
A2
A10
B2
B8
B12
to look like
A01
A02
A10
B02
B08
B12
using awk (6 Replies)
Discussion started by: mykey242
6 Replies
5. Shell Programming and Scripting
Hi all
Is there a way to pad the output of a bash script
see that code below
for i in `sed -n '/Start Printer/,/End Printer/p' /u/ab/scripts/hosts.conf | awk '!/^#/ {print $2}' | egrep -v 'broke|primera' `; do
pages=`snmpget -Ov -v1 -c public $i sysLocation.0 | awk '{print $2}'`
... (3 Replies)
Discussion started by: ab52
3 Replies
6. Shell Programming and Scripting
Hello All,
I have this script to awk IP to new file.
#awk '/myip|yourip/ {sub(/...\....\....\..../, newip)}1' newip=$IP existing.txt > new.txt
When existing.txt has myip=192.168.123.123 and $IP has 192.168.12.12, the awk script is not working. But while I add zero left padding to $IP i.e,... (3 Replies)
Discussion started by: Shaan_Shaan
3 Replies
7. Shell Programming and Scripting
Hi everyone,
This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file):
ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies
8. Shell Programming and Scripting
Hi,
I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below...
if (start_new_rec=="true"){
exclude_user="false";
user=toupper($6);
match(user, "XXXXX.");
if (RSTART ==2 ) {
... (9 Replies)
Discussion started by: asandy1234
9 Replies
LEARN ABOUT PHP
oci_fetch_assoc
OCI_FETCH_ASSOC(3) OCI_FETCH_ASSOC(3)
oci_fetch_assoc - Returns the next row from a query as an associative array
SYNOPSIS
array oci_fetch_assoc (resource $statement)
DESCRIPTION
Returns an associative array containing the next result-set row of a query. Each array entry corresponds to a column of the row. This
function is typically called in a loop until it returns FALSE, indicating no more rows exist.
Calling oci_fetch_assoc(3) is identical to calling oci_fetch_array(3) with OCI_ASSOC + OCI_RETURN_NULLS.
PARAMETERS
o $statement
-A valid OCI8 statement identifier created by oci_parse(3) and executed by oci_execute(3), or a REF CURSOR statement identifier.
RETURN VALUES
Returns an associative array. If there are no more rows in the $statement then FALSE is returned.
EXAMPLES
Example #1
oci_fetch_assoc(3) Example
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, 'SELECT department_id, department_name FROM departments');
oci_execute($stid);
while (($row = oci_fetch_assoc($stid)) != false) {
echo $row['DEPARTMENT_ID'] . " " . $row['DEPARTMENT_NAME'] . "<br>
";
}
oci_free_statement($stid);
oci_close($conn);
?>
NOTES
Note
See oci_fetch_array(3) for more examples of fetching rows.
SEE ALSO
oci_fetch(3), oci_fetch_all(3), oci_fetch_array(3), oci_fetch_object(3), oci_fetch_row(3).
PHP Documentation Group OCI_FETCH_ASSOC(3)