Remove when date is higher


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Remove when date is higher
# 1  
Old 11-06-2014
Remove when date is higher

Dear Masters,

I need to eliminate lines from file input 2 when the date in column 1 more than date in column 1 in file input 1

input 1

Code:
20141101|USA|CANSEL|496420000
20141101|USA|CANUT|1069740000
20141101|USA|CANTENG|625920000
20141102|USA|CANUT|413180000
20141103|USA|CANSEL|1364245000
20141103|USA|CANSEL|336455000
20141103|USA|CANSEL|159070000
20141104|USA|CANSEL|1257290000
20141104|USA|CANUT|992000000
20141105|USA|CANUT|909955000

input 2

Code:
20141001|USA|CANSEL|496420000
20141001|USA|CANUT|1069740000
20141001|USA|CANTENG|625920000
20141002|USA|CANUT|413180000
20141003|USA|CANSEL|1364245000
20141004|USA|CANSEL|336455000
20141005|USA|CANSEL|159070000
20141010|USA|CANSEL|1257290000
20141020|USA|CANUT|992000000
20141028|USA|CANUT|909955000

Since the highest date in column 1 is 20141105 ~ (05) then I need to remove lines in column 2 when the date is higher than 05, so my output should like

Code:
20141001|USA|CANSEL|496420000
20141001|USA|CANUT|1069740000
20141001|USA|CANTENG|625920000
20141002|USA|CANUT|413180000
20141003|USA|CANSEL|1364245000
20141004|USA|CANSEL|336455000
20141005|USA|CANSEL|159070000

this lines will be excluded since the date is 10,20,28
Code:
20141010|USA|CANSEL|1257290000
20141020|USA|CANUT|992000000
20141028|USA|CANUT|909955000

thanks

Last edited by radius; 11-06-2014 at 01:43 AM..
# 2  
Old 11-06-2014
And what have you tried to solve this problem?
# 3  
Old 11-06-2014
I did these steps, (unluckily, too many steps)

Code:
awk  -F '|' 'BEGIN {OFS=FS="|"}{print substr($1, 7, 2),$0}' input1.txt > input_a1.txt
awk  -F '|' 'BEGIN {OFS=FS="|"}{print substr($1, 7, 2),$0}' input2.txt > input_b1.txt
awk -F'|' 'NR==FNR {h[$1] = $2; next} {FS=OFS="|";print $0,h[$1]}' input_a1.txt input_b1.txt > a1b1.txt
awk '/\|$/ {next} 1' a1b1.txt > output.txt

too many steps happened..help me the shortest and best one
# 4  
Old 11-06-2014
Perhaps something more like:
Code:
awk '
FNR == NR {
	if(substr($0, 7, 2) > max) max = substr($0, 7, 2)
	next
}
substr($0, 7, 2) <= max' input[12].txt

would do what you want.
# 5  
Old 11-06-2014
None of the DATES in field 1 in file 2 (all Oct) is greater than any DATE in file 1 (all Nov), so I guess you talk of DAYS?
Try (in principle same as Don Cragun's proposal)
Code:
awk -F\| '{DAY=substr($0, 7, 2)} FNR==NR {MAX=(DAY>MAX)?DAY:MAX; next} (DAY<=MAX)' file[12]
20141001|USA|CANSEL|496420000
20141001|USA|CANUT|1069740000
20141001|USA|CANTENG|625920000
20141002|USA|CANUT|413180000
20141003|USA|CANSEL|1364245000
20141004|USA|CANSEL|336455000
20141005|USA|CANSEL|159070000


Last edited by RudiC; 11-06-2014 at 05:34 AM.. Reason: got the logics wrong; corrected
# 6  
Old 11-06-2014
"...I need to remove lines in column 2..." ????

I've no idea how to remove a 'line' from a 'column'.

Maybe I can guess what you meant to ask, but I think you need to fix your question.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Answers to Frequently Asked Questions

Compare date in .txt with system date and remove if it's lesser than system date

I m working on shell scripting and I m stuck where in my .txt file there is column as expiry date and I need to compare that date with system date and need to remove all the rows where expiry date is less than system date and create a new .txt with update. (1 Reply)
Discussion started by: Stuti
1 Replies

2. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

3. Shell Programming and Scripting

Checking for higher usage and mark it

Hi Gurus, I'm using HPUX B.11.23 U ia64 with sh shell. Is it possible to insert a word "Warning" in the end of this line if there is high percentage? For example: if the percentage is higher than 80%? Sample data: /dev/vgsap/TEST1 /oracle/TST/TEST1 9.89 GB 8.37 GB ... (11 Replies)
Discussion started by: superHonda123
11 Replies

4. UNIX for Dummies Questions & Answers

grep 2000 and higher

i have content that looks like this: 0003326050 A E LITHO 0023823422 AMERICAN RED CROSS 0005713642 ARUP LABORATORIES 0003206450 CAEL 0002519930 CARDINAL HEALTH 0002619063 FISHER HEALTHCAR 0065203177 OWENS & MINOR INC 0016552938 STAPLES INC 0000002001 MSC... (8 Replies)
Discussion started by: tjmannonline
8 Replies

5. UNIX for Dummies Questions & Answers

To remove files before certain date

I juz started to pick up unix nott long. What i am gonna do here is to try and remove some files before a date. (example 1st Oct 2008) Format of files name: fileA_2008MMDD I did a ls -lrt to list all the files Followed by rm 200801** .. .. ..... (4 Replies)
Discussion started by: Attitude
4 Replies

6. Solaris

Remove logs by date

Hi I Need To Remove Logs Older Than April 1 St(4/1/2007) By Date I Have A Script To Remove 3 Months Old Like Using 90 Days Thanks (3 Replies)
Discussion started by: cvdev
3 Replies

7. UNIX for Dummies Questions & Answers

Remove the date from the file

Hi I am getting the file which is having the format as follows: set-I abcxyz20080109 abc20080110 Set-II abc-20070109 abcxyz-20070110 I need to get only the file name without date as Set-I abcxyz abc Set-II abc (5 Replies)
Discussion started by: pradkumar
5 Replies

8. Shell Programming and Scripting

Remove out date logs

hi all, i would like to write the shell script to remove the out-dated log my log file name will be like this: access_20050101.log access_20050102.log . . . access_20071007.log access_20071008.log access_20071009.log i has try to write the command as following, it will be remove the... (2 Replies)
Discussion started by: eric_wong_ch
2 Replies

9. Shell Programming and Scripting

how to remove files with a date

I have files with a date name ( 20060506 20060507 etc..) that i want to remove because it keeps filling up the directory. Can someone please help me with a script to remove those date files. i would like to keep atleast 14 days worth from the current date. I hope i have explained it clearly and... (5 Replies)
Discussion started by: justintime
5 Replies
Login or Register to Ask a Question