substitute part of a regex
[aix] hi, i would like to modify portions of a file that look like ths so that the month is incremented by 1
Code:
DTP*434*RD8*20110714-20110716 Code:
sed s/DTP.472.RD8.2011..../DTP*472*RD8*201108../ date.txt |
It is difficult to understand, so give the expected OUTPUT.
|
If I understand correctly, you want to modify date codes---example:
OLD: 20110723 NEW: 20110823 For a completely general solution, you cannot do that directly with SED, unless you plan to enter each substitution by hand**. For a general solution, I would see something like this (psedocode): Code:
In a loop: A less robust solution: Code:
sed 's/201107/201108/g' oldfile > newfile |
Brute force ...
Code:
sed 's/201112/201201/g' < $InFile \ |
Hi.
Here is another sed solution: Code:
#!/bin/sed -rf @Daniel: quite clever and simple! It would probably be faster to make all substitutions by single sed process than using pipes. Anyway it should be much faster than mine. EDIT: I just realized that my script increments only the month, so 201112 incremented to 201101... This is a bug :) To solve this, one may add Code:
s/%01/201201/g |
How about:
Code:
#!/bin/bash |
Yes, I was also thinking about using date:
Code:
$ sed -r 's/^/echo /; s/([*-])([0-9]{8})/\1$(date -d "\2 +1 month" "+%Y%m%d")/eg' infile |
hi, i ended up piping about 10 sed substitutes (there was one date fror 2010). i figured changing the month was easier than incrementig the day because i wouldn't have to worry about the 30th/31st (or 28th/29th) as much.
also, aix's version of date does not have the -d parameter. thanks, i'm sure i'll have to revisit this in the future so now i have some reference. |
NB: Besides compiling the GNU coreutils on AIX, you can check whether the admin installed them already at a location like /opt/freeware/bin as IBM offers the AIX toolbox with these.
|
Quote:
Code:
sed 's/201112/201201/g; |
different question but why is this:
Code:
sed s/hello/world/ < file.txt Code:
sed s/hello/world/ file.txt |
Quote:
Daniel B. Martin |
All times are GMT -5. The time now is 06:41 PM. |