ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
read in a line
use SED, Grep, Awk to extract the month into a variable
increment the variable by 1
insert the new value using SED
(return to the beginning of the loop)
**If you want to do each substitution by hand, you might as well just use a text editor
At the first line of the script we append the lookup table delimited from other text by space. :a is a label to jump to later. In the third line we search for two characters after 2011, search these two characters in lookup table and replace characters after a year by next two characters in the lookup table. I also replace a year by the % sign to mark already processed dates. If last substitution was successful, jump to label a. Otherwise (all dates on the line are processed) remove lookup table and substitute all %-s by 2011.
@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
#!/bin/bash
while read -r line
do
IFS='*-' && set -- $line && unset IFS
if (( $# == 5 ))
then
line=${line/$4/$(date -d"$4 +1 month" "+%Y%m%d")}
line=${line/$5/$(date -d"$5 +1 month" "+%Y%m%d")}
fi
echo $line
done<date.txt >temp.txt
if ! diff -q date.txt temp.txt &>/dev/null
then
mv temp.txt date.txt
fi
This also does not suffer from needing to change the year if required
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.