05-20-2008
Awk - Working with fixed length files
OK I am somewhat new to UNIX programming please see what you can do to help.
I have a flat file that is a fixed length file containing different records based on the 1st character of each line. The 1st number at the beginning of the line is the record number, in this case it's record #1.
I need a slick way to change what's in columns 375-389 (currently: 200607211838001) with the 8 byte date, followed by the julian date followed by a 4 digit incremented sequence number (date +%Y%m%d%j$$$$) example: 200805201410001
Before:
1............................200607211838001..............................
2..................................................................................
1............................200607211838001..............................
2..................................................................................
1............................200607211838001..............................
2..................................................................................
1............................200607211838001..............................
After:
1............................200805201410001..............................
2..................................................................................
1............................200805201410002..............................
2..................................................................................
1............................200805201410003..............................
2..................................................................................
1............................200805201410004..............................
I tried this and this does not work because it's stomping over itself in the loop and setting everything to the last incremented value:
z=0
cat ${file}|grep ^1|while read n
do
z=$(( ${z} + 1 ))
z=`awk 'BEGIN {printf("%0.4d\n", '${z}' )}'`
trans=`date +%Y%m%d%j${z}`
awk '{x=1;if (substr($0,1,1)==x ) print substr($0,1,374) "'"${trans}"'" substr($0,390)} {if (substr($0,1,1)!=x ) print }' ${1} > ${1}.new;mv ${1}.new ${1}
done
Again any help would be greatly appreciated.
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Is there a command that sets a variable length?
I have a input of a variable length field but my output for that field needs to be set to 32 char.
Is there such a command?
I am on a sun box running ksh
Thanks (2 Replies)
Discussion started by: r1500
2 Replies
2. Shell Programming and Scripting
Hi
How to sort a fixed length file on a given char range and just display the duplicates.
I did search for man sort to find any option but could find any.,something similar to cut -c 1-5,25-35.
I have alternate way of doing this by using combination of cut,awk. but this
creates extra temp... (6 Replies)
Discussion started by: sach_in
6 Replies
3. Shell Programming and Scripting
I am trying to display df -h command out in proper format, how can I display
each field of each record in a fixed length. (2 Replies)
Discussion started by: roopla
2 Replies
4. Shell Programming and Scripting
Hi Unix Champs,
I want to awk on a fixed length file.
Instead if the file was a delimited file, then I could have used -F and then could have easily done manipulation on the fields.
How do i do the same in case of fixed length file?
Thanks in Advance.
Regards. (7 Replies)
Discussion started by: c2b2
7 Replies
5. Shell Programming and Scripting
Hi,
Can we join two fixed length files in Unix using JOIN command? Is there any other command to accomplish the same?
Thanks,
G.Harikrishnan (6 Replies)
Discussion started by: gharikrishnan
6 Replies
6. Shell Programming and Scripting
I have a number of unix text files containing fixed-length records (normal unix linefeed terminator) where I need to find odd records which are an incorrect length.
The data is not validated and records can contain odd backslash characters and control characters which makes them awkward to process... (2 Replies)
Discussion started by: methyl
2 Replies
7. Shell Programming and Scripting
Need a script that manipulates a fixed length file that will compare 2 fields in that file and if they are equal write that line to a new file.
i.e. If fields 87-93 = fields 119-125, then write the entire line to a new file. Do this for every line in the file. After we get only the fields... (1 Reply)
Discussion started by: Muga801
1 Replies
8. Shell Programming and Scripting
I am very new to scripting and need to write a script that will extract the account number from a line that begins with HDR. For example, the file is as follows
HDR2010072600300405505100726 00300405505
LBJ FREEWAY DALLAS
TELEGRAPH ... (9 Replies)
Discussion started by: bds052189
9 Replies
9. Shell Programming and Scripting
Hi,
I am in a situation to print the message on a column, where the each line starting position should be same.
For example code:
HOSTNAME1="1.2.3.4.5.6.7"
TARGET_DIR="/tmp"
echo "HOSTNAME1:" "$HOSTNAME1" | awk -v var="Everyone" '{len=55-length;printf("%s%*s\n",$0,len,var)}'
echo... (4 Replies)
Discussion started by: tprabhaker
4 Replies
10. Shell Programming and Scripting
Hi,
I need to split a fixed length file of 160 characters based on value of a column. Example:
ABC 456780001 DGDG SDFSF
BCD 444440002 SSSS TTTTT
ABC 777750003 HHHH UUUUU
THH 888880001 FFFF LLLLLL
HHH 999990002 GGGG OOOOO
I need to split this file on basis of column from... (7 Replies)
Discussion started by: Neelkanth
7 Replies
RECNO(3) Library Functions Manual RECNO(3)
NAME
recno - record number database access method
SYNOPSIS
#include <sys/types.h>
#include <db.h>
DESCRIPTION
The routine dbopen is the library interface to database files. One of the supported file formats is record number files. The general
description of the database access methods is in dbopen(3), this manual page describes only the recno specific information.
The record number data structure is either variable or fixed-length records stored in a flat-file format, accessed by the logical record
number. The existence of record number five implies the existence of records one through four, and the deletion of record number one
causes record number five to be renumbered to record number four, as well as the cursor, if positioned after record number one, to shift
down one record.
The recno access method specific data structure provided to dbopen is defined in the <db.h> include file as follows:
typedef struct {
u_long flags;
u_int cachesize;
u_int psize;
int lorder;
size_t reclen;
u_char bval;
char *bfname;
} RECNOINFO;
The elements of this structure are defined as follows:
flags The flag value is specified by or'ing any of the following values:
R_FIXEDLEN
The records are fixed-length, not byte delimited. The structure element reclen specifies the length of the record, and the
structure element bval is used as the pad character. Any records, inserted into the database, that are less than reclen
bytes long are automatically padded.
R_NOKEY
In the interface specified by dbopen, the sequential record retrieval fills in both the caller's key and data structures. If
the R_NOKEY flag is specified, the cursor routines are not required to fill in the key structure. This permits applications
to retrieve records at the end of files without reading all of the intervening records.
R_SNAPSHOT
This flag requires that a snapshot of the file be taken when dbopen is called, instead of permitting any unmodified records
to be read from the original file.
cachesize
A suggested maximum size, in bytes, of the memory cache. This value is only advisory, and the access method will allocate more mem-
ory rather than fail. If cachesize is 0 (no size is specified) a default cache is used.
psize The recno access method stores the in-memory copies of its records in a btree. This value is the size (in bytes) of the pages used
for nodes in that tree. If psize is 0 (no page size is specified) a page size is chosen based on the underlying file system I/O
block size. See btree(3) for more information.
lorder The byte order for integers in the stored database metadata. The number should represent the order as an integer; for example, big
endian order would be the number 4,321. If lorder is 0 (no order is specified) the current host order is used.
reclen The length of a fixed-length record.
bval The delimiting byte to be used to mark the end of a record for variable-length records, and the pad character for fixed-length
records. If no value is specified, newlines (``
'') are used to mark the end of variable-length records and fixed-length records
are padded with spaces.
bfname The recno access method stores the in-memory copies of its records in a btree. If bfname is non-NULL, it specifies the name of the
btree file, as if specified as the file name for a dbopen of a btree file.
The data part of the key/data pair used by the recno access method is the same as other access methods. The key is different. The data
field of the key should be a pointer to a memory location of type recno_t, as defined in the <db.h> include file. This type is normally
the largest unsigned integral type available to the implementation. The size field of the key should be the size of that type.
Because there can be no meta-data associated with the underlying recno access method files, any changes made to the default values (e.g.
fixed record length or byte separator value) must be explicitly specified each time the file is opened.
In the interface specified by dbopen, using the put interface to create a new record will cause the creation of multiple, empty records if
the record number is more than one greater than the largest record currently in the database.
ERRORS
The recno access method routines may fail and set errno for any of the errors specified for the library routine dbopen(3) or the following:
[EINVAL]
An attempt was made to add a record to a fixed-length database that was too large to fit.
SEE ALSO
btree(3) dbopen(3), hash(3), mpool(3),
Document Processing in a Relational Database System, Michael Stonebraker, Heidi Stettner, Joseph Kalash, Antonin Guttman, Nadene Lynn, Mem-
orandum No. UCB/ERL M82/32, May 1982.
BUGS
Only big and little endian byte order is supported.
4.4 Berkeley Distribution 1994-08-18 RECNO(3)