Sponsored Content
Full Discussion: Additonal Line in Text File
Top Forums UNIX for Dummies Questions & Answers Additonal Line in Text File Post 302581587 by methyl on Tuesday 13th of December 2011 12:28:56 PM
Old 12-13-2011
Just for interest. Open logic Shell version. Should find most situations where records are out of place.

Code:
#!/bin/ksh
recno=0         # Record number
recid=""        # Record ID 1/2/3/4
prev_recid="0"  # Previous record ID
#
cat filename.txt | while read line
do
        recno=$(( $recno + 1 ))
        recid=$(echo "${line}"|cut -c1-1)
        case "${recid}" in
                "1")
                        prev_recid="${recid}"
                        continue
                        ;;
                "2")
                        if [ "${prev_recid}" = "1" ]
                        then
                                prev_recid="${recid}"
                                continue
                        fi
                        if [ "${prev_recid}" = "3" ]
                        then
                                prev_recid="${recid}"
                                continue
                        fi
                        # Error. Do not use for subsequent compares
                        echo "Out of sequence : record ${recno}"
                        echo "${line}"
                        continue
                        ;;
                "3")
                        if [ "${prev_recid}" = "2" ]
                        then
                                prev_recid="${recid}"
                                continue
                        fi
                        # Error. Do not use for subsequent compares
                        echo "Out of sequence : record ${recno}"
                        echo "${line}"
                        continue
                        ;;
                "4")
                        prev_recid="${recid}"
                        continue
                        ;;
                *)
                        # Error. Do not use for subsequent compares
                        echo "Unknown record type : record ${recno}"
                        echo "${line}"
                        continue
                        ;;
        esac
done

./script.sh
Out of sequence : record 12
3PLAYER25 000011000003000


Last edited by methyl; 12-13-2011 at 01:32 PM.. Reason: paste error
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

2. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

3. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

4. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

5. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

6. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

7. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

9. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

10. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies
RECNO(3)						     Linux Programmer's Manual							  RECNO(3)

NAME
recno - record number database access method SYNOPSIS
#include <sys/types.h> #include <db.h> DESCRIPTION
Note well: This page documents interfaces provided in glibc up until version 2.1. Since version 2.2, glibc no longer provides these inter- faces. Probably, you are looking for the APIs provided by the libdb library instead. The routine dbopen(3) 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(3) is defined in the <db.h> include file as follows: typedef struct { unsigned long flags; unsigned int cachesize; unsigned int psize; int lorder; size_t reclen; unsigned char bval; char *bfname; } RECNOINFO; The elements of this structure are defined as follows: flags The flag value is specified by ORing 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(3), 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 applica- tions 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(3) 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 filesystem 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 filename for a dbopen(3) 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 metadata 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(3), 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. BUGS
Only big and little endian byte order is supported. 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. COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. 4.4 Berkeley Distribution 2017-09-15 RECNO(3)
All times are GMT -4. The time now is 02:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy