Records Inserting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Records Inserting
# 1  
Old 02-19-2013
Hammer & Screwdriver Records Inserting

Hi Guys ,

I Need to insert records into a file just above the last row .

like i have a file which has records as shown below :

Code:
00012919 7836049      S
00012920 7836049      S
00012921 3828157      Y
00012922 3828157      Y
00012923 3828157      S
T005290070331000012923

i want to introduce the below records above the tail i.e. last row and also edit the last row which has the row number in the ending i.e.12923 here as shown above with the updated row number, two records i want to add are stored in a file :

Code:
00012923 3824017      Y
00012924 3820067      S

Output should be :
Code:
00012919 7836049      S
00012920 7836049      S
00012921 3828157      Y
00012922 3828157      Y
00012923 3828157      S
00012924 3824017      Y
00012925 3820067      S
T005290070331000012925

would appreciate if i get help here .

Last edited by robert89; 02-19-2013 at 08:47 PM..
# 2  
Old 02-19-2013
Try something like:
Code:
awk '
/^[0-9]/ && FNR == NR {
        # Copy lines from 1st file (before the last line) to the output.
        last = $1       # Save the sequence number in the 1st field.
        print
        next
}
/^F/ {  final = $0;next}        # Save the last line from the 1st file.
{       # Replace the seqnece number in lines from the 2nd file and print
        # the modified lines...
        printf("%08d%s\n", ++last, substr($0, 9))
}
END {   # Replace the sequence number at the end of the last record in the 1st
        # file with the last sequence number assigned to new records from the
        # 2nd file.
        printf("%s%08d\n", substr(final, 1, length(final) - 8), last)
}' file add

where file contains the original file contents, and add contains the records to be added.

(Note, on Solaris/SunOS systems, use /usr/xpg4/bin/awk or nawk instead of awk.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separate records of a file on 2 types of records

Hi I am new to shell programming in unix Please if I can provide help. I have a file structure of a header record and "N" detail records. The header record will be the total number of detail records I need to split the file in 2: One for the header Another for all detail records Could... (1 Reply)
Discussion started by: jamcogar
1 Replies

2. Shell Programming and Scripting

AWK: Help inserting records between various matches

Hello, my apologizes if the title is a bit confusing. I am currently working with a series of files that have the form: 2 3 7 17 21 However, I need to insert records such that I have: 0 0 1 0 2 1 3 1 4 0 5 0 6 0 7 1 .... And so on. Currently I have the... (2 Replies)
Discussion started by: Euler2
2 Replies

3. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

4. Shell Programming and Scripting

Deleting duplicate records from file 1 if records from file 2 match

I have 2 files "File 1" is delimited by ";" and "File 2" is delimited by "|". File 1 below (3 record shown): Doc1;03/01/2012;New York;6 Main Street;Mr. Smith 1;Mr. Jones Doc2;03/01/2012;Syracuse;876 Broadway;John Davis;Barbara Lull Doc3;03/01/2012;Buffalo;779 Old Windy Road;Charles... (2 Replies)
Discussion started by: vestport
2 Replies

5. Shell Programming and Scripting

Split records into multiple records

Hi All, I am trying to split a record into multiple records based on a value. Input.txt "A",1,0,10 "B",2,0,10,15,20 "C",3,11,14,16,19,21,23 "D",1,0,5 My desired output is: "A",1,0,10 "B",2,0,10 "B",2,15,20 "C",3,11,14 "C",3,16,19 "C",3,21,23 (4 Replies)
Discussion started by: kmsekhar
4 Replies

6. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

7. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies

8. Shell Programming and Scripting

Need help in inserting a value.........

Hello, I'm having a files with variable no of lines. An example for this file is as follows ~xxx STRT 0.0000000000 : STOP : ~xxxxx 0.000000 557.109552 -557.109552 1511.482910 -954.373377 954.373377 in the STOP line below the 0.00000000000 I need... (8 Replies)
Discussion started by: santyshyam
8 Replies

9. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies

10. Shell Programming and Scripting

Inserting records from flat file to db table

I have 20000 numbers present in a file in each line like 25663, 65465, 74579, 56446, .. .. I have created a table in db with single number column in it. create table testhari (no number(9)); I want to insert all these numbers into that table. how can i do it? can anybody please... (4 Replies)
Discussion started by: Hara
4 Replies
Login or Register to Ask a Question