Help in Editing Records of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in Editing Records of a file
# 1  
Old 02-22-2013
Wrench Help in Editing Records of a file

Hi Friends ,

Need help here , I Have a file which has as a number of rows (records) in it .
Code:
00012919 7836049      S
00012920 7836049      S
00012921 3828157      Y
00012922 3828157      Y
00012923 3828157      S
T005290070331000012923

I Have another file which has few more records :

Code:
00012956 7236046      S
00012942 3458235      Y

All I want is a code which could remove the last line i.e. tail of the first file and then insert the records from the second file and then again insert the tail which was removed earlier .

Output :

Code:
 
 
00012919 7836049      S
00012920 7836049      S
00012921 3828157      Y
00012922 3828157      Y
00012923 3828157      S
00012956 7236046      S
00012942 3458235      Y
T005290070331000012923


Please Help .
# 2  
Old 02-22-2013
Try this:
Code:
awk 's{print s}{s=$0} END{system("cat file2");print s}' file1

# 3  
Old 02-23-2013
A small script to do the same...

Code:
    
#!/bin/bash
#tailer.sh
        if [ $# -lt "2" ]; then
        {
                echo "Sorry you need to pass the name of the first file followed by the name of the second file."
        }
                elif ! [ -e "$1" ] || ! [ -e "$2" ]; then
        {
                echo "Sorry I couldn't find your files"
        }
        fi
        #create a variable with the last line of the file to be augmented
        this_tail=`cat $1 | grep -v ^$ | tail -n 1`
        #output the first file, minus the last line, followed by the second file, followed by the last line of the first.
        (cat $1 | grep -v ^$ | grep -v "$this_tail") && (cat $2 | grep -v ^$) && echo $this_tail
    
exit

and here is the example output
Code:
bash tailer.sh tailed.txt insert.txt
00012919 7836049      S
00012920 7836049      S
00012921 3828157      Y
00012922 3828157      Y
00012923 3828157      S
00012956 7236046      S
00012942 3458235      Y
T005290070331000012923

# 4  
Old 02-23-2013
Thanq guys , Thanks a lot .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert vi editing to text editing

Dear Guru's I'm using Putty and want to edit a file. I know we generally use vi editor to do it. As I'm not good in using vi editor, I want to convert the vi into something like text pad. Is there any option in Putty to do the same ? Thanks for your response. Srini (6 Replies)
Discussion started by: thummi9090
6 Replies

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

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

4. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

5. Shell Programming and Scripting

Help with file editing while keeping file format intact

Hi, I am having a file which is fix length and comma seperated. And I want to replace values for one column. I am reading file line by line in variable $LINE and then replacing the string. Problem is after changing value and writing new file temp5.txt, formating of original file is getting... (8 Replies)
Discussion started by: Mruda
8 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

Editing long records with characters that need to be escaped.

Hi all, I'm new in unix scripting and I've a problem with a script... :confused: I need to read a file, add some fields in the records, and write them in another file, but even when I simply read and write the records, the shell interprets some caracters and the result is that the records... (5 Replies)
Discussion started by: Macs_Linux
5 Replies

8. UNIX for Dummies Questions & Answers

Use records from one file to delete records in another file

file_in_1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 file_in_2: 9 10 11 12 21 22 23 24 1 2 3 4 17 18 19 20 file_out: (5 Replies)
Discussion started by: kenneth.mcbride
5 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. UNIX for Advanced & Expert Users

Editing the end of the file without loading the entire file

hi! I am a newbee. I would really appreciate if you can answer the following question: I have a huge data file, 214MB with several coloumns. I need to delete the very last line of the file. Everything I know takes a lot of time to do it ( because I have to open the file in an editor or run a... (3 Replies)
Discussion started by: Garuda
3 Replies
Login or Register to Ask a Question