tail from one and match it in the other file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers tail from one and match it in the other file
# 1  
Old 05-16-2012
tail from one and match it in the other file

Can someone help me to do the following

I have two files

file one

Code:
123456
789012
345678
901234
567890

file two

Code:
abcdef
ghklmn
opqrst
901234
567890
uvwxyz
123456

I would like to do tail on the last two lines from file one,
match the string in file two and then delete all lines above matching string, and send output to file three.

File three should look

Code:
uvwxyz
123456

My search is always based on the last two lines from file one, and that changes every day.

Thank you for your help.

---------- Post updated at 07:32 PM ---------- Previous update was at 07:29 PM ----------

And I forgot to mention, I searched forum and could not find any previos topics that could provide the solution to my issue.

Last edited by Scrutinizer; 05-17-2012 at 05:22 AM.. Reason: code tags
# 2  
Old 05-17-2012
this seems to work:

Code:
$ awk 'FNR==NR{a=b;b=$0;next}p{print;next}$0==b&&a2==a{p=1}{a2=$0}' file1 file2
uvwxyz
123456

FNR==NR is true for first file. so while reading first file we end with a being next to last line, and b being the last line. Then while reading file2 we search for a line that matches b and check that the previous one also matches a and print everything after it.
This User Gave Thanks to neutronscott For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget)... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

4. Shell Programming and Scripting

Joining multiple files tail on tail

I have 250 files that have 16 columns each - all numbered as follows stat.1000, stat.1001, stat.1002, stat.1003....stat.1250. I would like to join all 250 of them together tail by tail as follows. For example stat.1000 a b c d e f stat.1001 g h i j k l So that my output... (2 Replies)
Discussion started by: kayak
2 Replies

5. Shell Programming and Scripting

How to tail -f real time file.

How to tail -f real time file. I want to tail file created last time. The server is gen new file Always. . An example file. -rw-r--r-- 1 shinnie tiituck 251M Oct 18 05:39 20111018_00.log -rw-r--r-- 1 shinnie tiituck 251M Oct 18 11:18 20111018_01.log -rw-r--r-- 1 shinnie tiituck... (3 Replies)
Discussion started by: ooilinlove
3 Replies

6. UNIX for Dummies Questions & Answers

Tail command with wildcard file name

Please help with the following command tail -f /appdata/logs/alert_audit517.txt | grep "Sep 02" The problem I have is with the file name "alert_audit517.txt". The 3 digit number at the end of the file name changes, so I need the file name to use a wildcard. Ive tried alert_audit***.txt, but... (5 Replies)
Discussion started by: robertson1995
5 Replies

7. Shell Programming and Scripting

how to tail a file in perl

Can any one please help why does tail -f does not work in the below code: Thanks in advance. #!/usr/bin/perl -w my $path = "/home/cs/logs/"; my $log = "log.txt"; `cd $path`; `tail -f $log`; (3 Replies)
Discussion started by: sureshcisco
3 Replies

8. Shell Programming and Scripting

How do I get my script to monitor a new file using tail?

Hi, I have a script which basically watches a log file for new lines using tail, then takes action based on what is logged. I wrote a script to do this for me and its working great, my only problem is that once per week, this log file is archived to another directory, and a new log is created.... (4 Replies)
Discussion started by: lstorm2003
4 Replies

9. Shell Programming and Scripting

how to tail a log file..

Hi All.. I have a log file in which all the backup information is stored. Now i have written a script which get the last line in the backup log file.. ssh -l ora${sid} ${primaryhost} "tail -1 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" However i would like to tail the line last... (4 Replies)
Discussion started by: suri.tyson
4 Replies

10. Linux

tail most recent file command

I have only been working with Linux for a few years now so bear with my noob question. I was wondering if there is a way to tail the most recent file that has a file name like 'scrubsncoa%'. There will be at least 2 files in the directory that start with 'scrubsncoa' and a few other different... (2 Replies)
Discussion started by: RyanD
2 Replies
Login or Register to Ask a Question