conditional replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting conditional replacement
# 1  
Old 11-26-2010
conditional replacement

Hi all,

I need a bash, sed, awk script or one liner to do the following task:

This is the format of a text file:
Code:
2010-06-11 20:01 902656 HOP-W-100412-1.doc
2010-11-05 18:01 364447 NEX-W-101104-1
2010-07-06 10:01 64512 Cerintele
2010-07-06 10:01 599420 content
2010-07-19 14:01 1785344 specifications.ppt
2010-05-03 13:01 542673 Attends
...

I need to delete from this file the lines where the date and time in the first two columns are older then a given date.
I think may be a good solution to replace the date and time with the format of date in seconds since 1970-01-01 00:00:00.
Code:
cut -d" " -f1,2 textfile.txt | date -f - +$s

This command is working, but I don't know how to replace the output in place.

After that may be using some kind of conditional grep...
Code:
grep -v ($1 < $myconstant) textfile.txt

- I know, this command isn't exist, just an example...

Thank you in advance,

vazi

Last edited by vbe; 11-26-2010 at 07:47 AM.. Reason: code tags please
# 2  
Old 11-26-2010
Try:
Code:
awk -F- '$1$2$3>=d' d=20100719 infile

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-26-2010
I don't know how to use your example, it's not doing anything. It's just echoing the infile...
# 4  
Old 11-26-2010
If I apply this to your file it deletes every line that is older than 20100719
Code:
$ awk -F- '$1$2$3>=d' d=20100719 infile
2010-11-05 18:01 364447 NEX-W-101104-1
2010-07-19 14:01 1785344 specifications.ppt

You can use any date
Code:
$ awk -F- '$1$2$3>=d' d=20101105 infile
2010-11-05 18:01 364447 NEX-W-101104-1

# 5  
Old 11-26-2010
Ah, sorry... I see now.

But I need to put in game the time entry too in the second column.

Thanks,
# 6  
Old 11-26-2010
It seemed to me time does not really matter if your are deleting posts after a certain date.

If you want to delete everything older than a specific point in time you could do this:
Code:
$ awk -F'[-: \t]' '$1$2$3$4$5>=d' d=201007191500 infile
2010-11-05 18:01 364447 NEX-W-101104-1

Code:
$ awk -F'[-: \t]' '$1$2$3$4$5>=d' d=201007191400 infile584
2010-11-05 18:01 364447 NEX-W-101104-1
2010-07-19 14:01 1785344 specifications.ppt


Last edited by Scrutinizer; 11-26-2010 at 09:17 AM..
# 7  
Old 11-26-2010
Seams to work for me.

Thank you wm,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional replacement in CSV files

Hello, I have many CSV files with variable number of rows and columns. Sample of few problematic CSV files. ,,Price,Price,Price,Price,Price,Price,Price,Price,Price,Qty Date,Sl,AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,PriQueue,%busy 30/07/2014,1,AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,NA,0... (8 Replies)
Discussion started by: reddyr
8 Replies

2. Shell Programming and Scripting

Conditional replacement of columns in a text file

Hello scriping expert friends, I have 2 requirements on replacing fields of text files: I have lot of data with contents like below: Requirement-1: The digit after 0 should always be changed to 1 (3 Replies)
Discussion started by: magnus29
3 Replies

3. Shell Programming and Scripting

Conditional replacement of a delimiter

Hello, I'm new to this forum but this seems like the place to ask this question. I have a pipe delimited data file with the fields except for the header being encased in double quotes. I found out that some of the fields have an trash pipe within the data itself. I'd like to conditionally... (4 Replies)
Discussion started by: samahs
4 Replies

4. Shell Programming and Scripting

Conditional tab replacement sed/awk

Hi I am struggling to find a solutions to this problem: I have a directory full of files and I wish to: read each line of each file and if any one line in those files is longer than 72 characters I want to replace any tab characters with a space character. Ive been... (3 Replies)
Discussion started by: benackland
3 Replies

5. Shell Programming and Scripting

HELP Need in SED/PERL conditional line replacement

Hi , I need some help on perl/sed conditional replacement The situation is like below . I have a file contents like below . AAA|BBB|CCC|DDD AAA|BCF|CCC|HHH AAA|BVF|JJJ|KKK Here in the above file . I know my second column value (taking "|" as my delimited ) Basically I have to... (3 Replies)
Discussion started by: robin.r888
3 Replies

6. Shell Programming and Scripting

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (12 Replies)
Discussion started by: abhinavsinha
12 Replies

7. UNIX for Dummies Questions & Answers

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (1 Reply)
Discussion started by: abhinavsinha
1 Replies

8. UNIX for Dummies Questions & Answers

conditional

conditional is not wworking can any one figure out what goes wrong xx1=`$ORACLE_HOME/bin/sqlplus -s apps/ostgapps1 2>/dev/null << EOF WHENEVER SQLERROR EXIT 1 set head off feedback off ; WHENEVER SQLERROR EXIT SQL.SQLCODE; select count(*) from CMS_INVOICE_ALL... (2 Replies)
Discussion started by: u263066
2 Replies

9. Shell Programming and Scripting

AWK - conditional cause

Hello guys, I want to make a conditional cause in the following file using awk: awk '{ if ($2 != 0) print $1, $2, $3}' test.csv > test2.csv FILE EXAMPLE = test.csv string,number,date abc,0,20050101 def,1,20060101 ghi,2,20040101 jkl,12,20090101 mno,123,20020101 ... (2 Replies)
Discussion started by: Rafael.Buria
2 Replies

10. Shell Programming and Scripting

Conditional Cron

Hi, The following test in cron fails. Any clue? I want it to run last day before end of month 34 13 * * * && /export/myshell.sh Regards (4 Replies)
Discussion started by: baanprog
4 Replies
Login or Register to Ask a Question