Remove when substring meets condition


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Remove when substring meets condition
# 1  
Old 10-07-2014
Remove when substring meets condition

Hi Masters,

I need to remove lines when date format is below certain date



My file input
Code:
20140906|ALASKA|USASEL|TARPUNG|2014-03-01|82176614040|20|1
20140906|ALASKA|USATENG|CRUIEX|2014-08-01|81267079997|5|0
20140906|ALASKA|USASEL|CRUIEMBANG|2013-10-01|82280779814|9|0
20140906|ALASKA|USAUT|AISTE|2014-06-01|81375325886|113|1
20140906|ALASKA|USATENG|CRUIEX|2014-10-05|81268573521|17|0

Since this month is October, so I want to print lines when column 5 with date 2014-10

My Expected Output should be
Code:
20140906|ALASKA|USASEL|CRUIEMBANG|2013-10-01|82280779814|9|0
20140906|ALASKA|USATENG|CRUIEX|2014-10-05|81268573521|17|0

I tried this before
Code:
PERIOD=`date +'%Y-%m'`
awk -F"|" '{FS=OFS="|'}{substr($5~$PERIOD)}' input.txt > output.txt

thanks a lot

Last edited by radius; 10-07-2014 at 07:24 AM..
# 2  
Old 10-07-2014
Quote:
Originally Posted by radius
Hi Masters,

I need to remove lines when date format is below certain date



My file input
Code:
20140906|ALASKA|USASEL|TARPUNG|2014-03-01|82176614040|20|1
20140906|ALASKA|USATENG|CRUIEX|2014-08-01|81267079997|5|0
20140906|ALASKA|USASEL|CRUIEMBANG|2013-10-01|82280779814|9|0
20140906|ALASKA|USAUT|AISTE|2014-06-01|81375325886|113|1
20140906|ALASKA|USATENG|CRUIEX|2014-10-05|81268573521|17|0

Since this month is October, so I want to print lines when column 5 with date 2014-10

My Expected Output should be
Code:
20140906|ALASKA|USASEL|CRUIEMBANG|2013-10-01|82280779814|9|0
20140906|ALASKA|USATENG|CRUIEX|2014-10-05|81268573521|17|0

I tried this before
Code:
PERIOD=`date +'%Y-%m'`
awk -F"|" '{FS=OFS="|'}{substr($5~$PERIOD)}' input.txt > output.txt

thanks a lot


if you want to exclude line other than current month you can do like this

Code:
awk '$2 == mon' FS='-' mon="$(date +"%m")" file

Code:
awk 'substr($5,6,2) == mon'  FS='|' mon="$(date +"%m")" file

For current year and month do something like this

Code:
awk 'substr($5,1,7) == yearmon'  FS='|' yearmon="$(date +'%Y-%m')" file


Last edited by Akshay Hegde; 10-07-2014 at 07:34 AM..
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 10-07-2014
Quote:
Originally Posted by radius
Hi Masters,

I need to remove lines when date format is below certain date



My file input
Code:
20140906|ALASKA|USASEL|TARPUNG|2014-03-01|82176614040|20|1
20140906|ALASKA|USATENG|CRUIEX|2014-08-01|81267079997|5|0
20140906|ALASKA|USASEL|CRUIEMBANG|2013-10-01|82280779814|9|0
20140906|ALASKA|USAUT|AISTE|2014-06-01|81375325886|113|1
20140906|ALASKA|USATENG|CRUIEX|2014-10-05|81268573521|17|0

Since this month is October, so I want to print lines when column 5 with date 2014-10

My Expected Output should be
Code:
20140906|ALASKA|USASEL|CRUIEMBANG|2013-10-01|82280779814|9|0
 
20140906|ALASKA|USATENG|CRUIEX|2014-10-05|81268573521|17|0

I tried this before
Code:
PERIOD=`date +'%Y-%m'`
awk -F"|" '{FS=OFS="|'}{substr($5~$PERIOD)}' input.txt > output.txt

thanks a lot
Hi Radius,

Following may help in same.
Code:
awk -F"|" -vvar=$(date +"%Y-%m") '{V=$5;gsub(/...$/,X,V);if(V == var) {print $0}}' OFS="|"  Input_file

Output will be as follows.
Code:
20140906|ALASKA|USATENG|CRUIEX|2014-10-05|81268573521|17|0

EDIT:
Quote:
Since this month is October, so I want to print lines when column 5 with date 2014-10

My Expected Output should be

Code:
20140906|ALASKA|USASEL|CRUIEMBANG|2013-10-01|82280779814|9|0
20140906|ALASKA|USATENG|CRUIEX|2014-10-05|81268573521|17|0
As per your condition it should be Oct 2014 so I guess above highlighted output showed by you is a typo.

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-07-2014 at 07:41 AM.. Reason: Added a quote in solution
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 10-07-2014
Mr Akhsya,..
im sorry about my expected output..supposed that line like 2013-10 should not be printed

expected ouput only 1 line based on my file input
Code:
20140906|ALASKA|USATENG|CRUIEX|2014-10-05|81268573521|17|0

i tried your code
Code:
awk 'substr($5,6,2) == mon'  FS='|' mon="$(date +"%Y-%m")" file

but it is still wrong..please help with awk

I tried what Mr Ravinder said and it works..thanks master
# 5  
Old 10-07-2014
Quote:
Originally Posted by radius
Mr Akhsya,..
im sorry about my expected output..supposed that line like 2013-10 should not be printed
expected ouput only 1 line based on my file input
Code:
20140906|ALASKA|USATENG|CRUIEX|2014-10-05|81268573521|17|0

i tried your code
Code:
awk 'substr($5,6,2) == mon'  FS='|' mon="$(date +"%Y-%m")" file

but it is still wrong..please help with awk
...


Hi radius please read post 2 once again, you may not noticed there are 2 solutions

Code:
awk 'substr($5,1,7) == yearmon'  FS='|' yearmon="$(date +'%Y-%m')" file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove lines matching a substring in a specific column

Dear group, I have following input text file: Brit 2016 11 18 12 00 10 1.485,00 EUR Brit 2016 11 18 12 00 10 142,64 EUR Brit 2016 11 18 12 00 10 19,80 EUR Brit 2016 11 18 12 00 10 545,00 EUR Brit 2016 11 18 12 00 10 6.450,00 EUR... (3 Replies)
Discussion started by: gfhsd
3 Replies

2. Shell Programming and Scripting

Using sed, awk or perl to remove substring of all lines except the first

Greetings All, I would like to find all occurences of a pattern and delete a substring from the all matching lines EXCEPT the first. For example: 1234::group:user1,user2,user3,blah1,blah2,blah3 2222::othergroup:user9,user8 4444::othergroup2:user3,blah,blah,user1 1234::group3:user5,user1 ... (11 Replies)
Discussion started by: jacksolm
11 Replies

3. Shell Programming and Scripting

Substring check in IF condition in shell script

I want to check if the string has the substring in IF condition then process... i tried below but not working if ]; then ............. field can be "reserved1" ....reservedn / fillspaces1 ... fillspacesn (4 Replies)
Discussion started by: greenworld123
4 Replies

4. Shell Programming and Scripting

Remove a substring from string

Good morning friends, how can i remove a string with linux scripting from a file? In specific i want to remove from a file all the tweet names and links eg @aerta and links such as http://dst.co/pIiu3i9c Thanx!!! (4 Replies)
Discussion started by: paladinaeon
4 Replies

5. Shell Programming and Scripting

Need to delete all lines where any line meets a condition

Here is an example of a file... foo1,good foo1,good foo2,error foo2,good Note that both rows for foo1 have good in the 2nd field, but one of the foo2 rows has error... I need something in ksh/awk/perl that will delete ALL foo2 lines if ANY of them have error in the 2nd field...so: ... (7 Replies)
Discussion started by: dbiggied
7 Replies

6. Shell Programming and Scripting

Remove duplicate line on condition

Hi Ive been scratching over this for some time with no solution. I have a file like this 1 bla bla 1 2 bla bla 2 4 bla bla 3 5 bla bla 1 6 bla bla 1 I want to remove consecutive occurrences of lines like bla bla 1, but the first column may be different. Any ideasss?? (23 Replies)
Discussion started by: jamie_123
23 Replies

7. Shell Programming and Scripting

how to remove this substring?

Hello, I need to remove a substring from a string in a ksh script, the string is like this: LOCATION=+DATADG1/CMSPRD1/datafile/system.235.456721 or LOCATION=/u03/oradata/CMSPRD/SYSTEM.dbf I need to strip the last file name from that path, and get: +DATADG1/CMSPRD1/datafile or,... (8 Replies)
Discussion started by: seafan
8 Replies

8. Shell Programming and Scripting

How to broadcast the message if any condition meets

Hi All, Can any1 help me out in broadcasting a message to all users if a condtion is meet. Like I am trying to get values from a directory for service monitoring. If a condition is meet it should broadcast the message. I try to use wall command but i m not sure how its works as its... (1 Reply)
Discussion started by: jojo123
1 Replies

9. Shell Programming and Scripting

how to remove a substring

I want to modify a string in shell script (/bin/sh). Could you please help me. I have a variable which can store the values like below. v10.5.2.1p001C_TN1 or v10.5.2.1p002_TN1 I have to search first whether ‘p' character exists into the string which is stored in a variable..... (5 Replies)
Discussion started by: samtekdallas
5 Replies

10. Shell Programming and Scripting

remove some files on a condition..

Hi.. when I do a ls -lt, I get a listing of about 200 files.. These are trace files and some of it I might not need.. To be clear, say in a given week , I might not need files that have been traced between 11 and 11:30 am on a particular day. How can I delete based on this condition ? Thanks,... (4 Replies)
Discussion started by: ST2000
4 Replies
Login or Register to Ask a Question