sed --> sort data by date

Hi,

i "tried" to sort data by date. So far, i used sed to take the data from the last and the actual month. Now, after changing the year it is not working properly.

i use:
GNU bash, version 4.2.45(1)-release (x86_64-suse-linux-gnu)

sed -n '/[0-3][0-9]\/['$y']/p' $Home/../scripte/pd_0.txt

y is a variable, build bei the shell-command y=`date +%y`

### data-format in pd_0.txt
2985,12/31/13,23:30,92637,9135,10018,9758,8504,6555,4573,2834,...
2986,12/31/13,23:40,97195,9281,10080,9868,8752,7091,4792,2951,...
2987,12/31/13,23:50,95258,9453,10164,10154,9285,7146,4919,2982...
2988,01/01/14,00:00,100358,9745,10622,10376,9291,7475,5222,3395,...
2989,01/01/14,00:10,128834,11704,13456,14235,13857,12485,10119,...
2990,01/01/14,00:20,165685,13613,16164,17818,18713,18242,15764,....

after all i'am very confused about. espacially why it works properly last year.
what i want to filter, ist the data from the last two month - e.g. from today, the data from Dec 2013 and the Data from Jan 2014.
Can someone help me please.

Thanks in advance,
IMPe

A best practice is to use the YYYYMMDD format ( date '+%Y%m%d' ) for the date, so that further sorting are made easy.

Not that YYYY is better than YY because retaining only the 2 last digit suppose a loss of information and may lead to ambiguous situations.

1 Like

Hi ctsgnb,

thanks for your fast repley.
what i forgot to say: the meassurement data and data-format is build by a device. it is not possible for me to change it. So i choose it this way, because the data-format is fix and csv: MM/DD/YY, hh:mm, 1234, 5678 .

Thanks in advance,
IMPe

To keep the same formatting:

sed 's;[:/];,;g' yourfile | sort -t, -k 4,4 -k 2,3 -k 5,6 | sed 's/,/:/5;s:,:/:3;s:,:/:2'

or move the YY at the beginning of the date so that the format will change into YY/MM/DD:

sed 's:\(../..\)/\(..\):\2/\1:' yourfile | sort -t, -k 2,3

As mentionned in my previous post, retaining only YY loose information and may lead to unexpected result (example: 12 may mean 1912 or 2012 or whatever ???12...)

---------- Post updated at 14:43 ---------- Previous update was at 13:07 ----------

I just got aware i've missunderstood your question:

Before running your

sed -n '/[0-3][0-9]\/['$y']/p' $Home/../scripte/pd_0.txt

You need to check wether you are in january : if so, the %y should be 134 rather than 14

if [ $(date +%m) = "01" ]
then
y=$(echo $(date +%y)$( echo $(($(date +%Y)-1 )) | tail -c 3 ) )
else
y=$(date +%y)
fi

should be sufficient since [1413] in regex will give the same result than [134] (it's just a little less clean)

By the way, you will have to care of century jump because your %y will go from 99 to 00, that's why i calculate last year using %Y (uppercase) rather than %y.

1 Like

Hi ctsgnb,

thanks a lot for your extensive support. It take some time for me, to understand all your methods and proposals. But again i have learned more about sed and i solved my problem properly.

Thanks a lot,
IMPe

Hi All,

Actually I am new to this Forum.Don't know how to post my question.

Can anyone help me :

we can replace dbSQL( with dbSQLWithBind( value by using :
:.,$s/dbSQL(/dbSQLWithBind(/g command inside a script.

For replacing ^M characters from a file, we normally use this command:

perl -i -p -e 's/^M//g' *.txt

But i am failing in replacing dbSQL( with dbSQLWithBind( value through perl command.
Please help me in replacing the value dbSQL( with dbSQLWithBind( globally in all scripts without opening the scripts.

for Vara Prasad:

cat file | sed 's/\^M//g'