batch file reverse list - list

#echo off
setlocal enabledelayedexpansion
chcp 65001
rem δημιουργία λίστας
set a[0]=μαρία
set a[1]=ηλιάννα
set a[2]=μιχάλης
set a[3]=γιώργος
set a[4]=κώστας
with the list above and with the for command I want to have the following result:
μαρία κώστας
ηλιάννα γιώργος
μιχάλης μιχάλης
γιώργος ηλιάννα
κώστας μαρία
I have tried
#echo off
setlocal enabledelayedexpansion
chcp 65001
set list=4
set a[0]=μαρία
set a[1]=ηλιάννα
set a[2]=μιχάλης
set a[3]=γιώργος
set a[4]=κώστας
for /L %%i in (0,1,4) do (
for /L %%x in (4,-1,0) do (
echo !a[%%i]! , !a[%%x]!))
but i get as a result :
μαρία , κώστας
μαρία , γιώργος
μαρία , μιχάλης
μαρία , ηλιάννα
μαρία , μαρία
ηλιάννα , κώστας
ηλιάννα , γιώργος
ηλιάννα , μιχάλης
ηλιάννα , ηλιάννα
ηλιάννα , μαρία
μιχάλης , κώστας
μιχάλης , γιώργος
μιχάλης , μιχάλης
μιχάλης , ηλιάννα
μιχάλης , μαρία
γιώργος , κώστας
γιώργος , γιώργος
γιώργος , μιχάλης
γιώργος , ηλιάννα
γιώργος , μαρία
κώστας , κώστας
κώστας , γιώργος
κώστας , μιχάλης
κώστας , ηλιάννα
κώστας , μαρία

for /L %%i in (0,1,4) do (
set /a pair=4-%%i
FOR %%x IN (!pair!) DO echo !a[%%i]! , !a[%%x]!
)
should do what you require

Related

Django ORM: Sum value and then create extra field showing rolling average

Is it possible, using Django's ORM to create a Sum based on a Date column and then add an extra field with a rolling average.
Let's say I have a table like this, called "Sales":
|---------------------|------------------|--------------|
| Date | Category | Value |
|---------------------|------------------|--------------|
| 2020-04-01 | 1 | 55.0 |
|---------------------|------------------|--------------|
| 2020-04-01 | 2 | 30.0 |
|---------------------|------------------|--------------|
| 2020-04-02 | 1 | 25.0 |
|---------------------|------------------|--------------|
| 2020-04-02 | 2 | 85.0 |
|---------------------|------------------|--------------|
| 2020-04-03 | 1 | 60.0 |
|---------------------|------------------|--------------|
| 2020-04-03 | 2 | 30.0 |
|---------------------|------------------|--------------|
I would like to group it by Date (column "Category" is unimportant) and add the Sum of the values for the date. Then I would like to add a rolling Average for the last 7 days.
I tried this:
days = (
Sales.objects.values('date').annotate(sum_for_date=Sum('value'))
).annotate(
rolling_avg=Window(
expression=Avg('sum_for_date'),
frame=RowRange(start=-7,end=0),
order_by=F('date').asc(),
)
)
.order_by('date')
This throws this error:
django.core.exceptions.FieldError: Cannot compute Avg('sum_for_date'): 'sum_for_date' is an aggregate
Any ideas?

DAX: Return distinct column values based on other conditions

I have a table whose relevant columns are: ProjectName, TaskNo, AssignedTo, and Status. The data looks something like this:
--------------------------------------------------------------
| ProjectName | TaskNo | AssignedTo | Status |
+-----------------+------------+----------------+------------+
| Project1 | 00300 | Database | In Process |
+-----------------+------------+----------------+------------+
| Project2 | 02200 | Alfred | In Process |
+-----------------+------------+----------------+------------+
| Project2 | 00300 | Database | In Process |
+-----------------+------------+----------------+------------+
| Project3 | 02200 | Alfred | Complete |
+-----------------+------------+----------------+------------+
| Project3 | 00900 | Database | In Process |
+-----------------+------------+----------------+------------+
| Project4 | 02200 | Alfred | Complete |
+-----------------+------------+----------------+------------+
| Project4 | 01200 | Database | Complete |
+-----------------+------------+----------------+------------+
| Project4 | 00300 | Database |Not Started |
+-----------------+------------+----------------+------------+
There are some two dozen task numbers per project that are assigned to many different groups.
I am looking to output a list of ProjectName where
TaskNo 02200 has status Complete AND
There is at least 1 task assigned to Database that is not Complete.
From the table above, my desired output would be:
-------------------
| ProjectName |
+-----------------+
| Project3 |
+-----------------+
| Project4 |
-------------------
I have managed to filter the very large list down somewhat - to Completed 02200's and incomplete Database tasks with this FILTER expression:
FILTER (
'Project Tasks',
OR (
'Project Tasks'[Title] = "02200"
&& 'Project Tasks'[Status] = "Completed",
'Project Tasks'[GroupAssignedTo] = "Database"
&& 'Project Tasks'[Status] <> "Completed"
)
)
I can't seem to take it the rest of the way - to find within this filtered table that satisfy the two conditions.
The trick is that you need to consider each ProjectName at a group rather than just row by row.
To do that, let's use the SUMMARIZE function:
FilteredProjects =
VAR Summary =
SUMMARIZE (
'Project Tasks',
'Project Tasks'[ProjectName],
"02200 Complete", COUNTROWS (
FILTER (
'Project Tasks',
'Project Tasks'[TaskNo] = "02200"
&& 'Project Tasks'[Status] = "Complete"
)
) > 0,
"Database Not Complete", COUNTROWS (
FILTER (
'Project Tasks',
'Project Tasks'[AssignedTo] = "Database"
&& 'Project Tasks'[Status] <> "Complete"
)
) > 0
)
RETURN
SELECTCOLUMNS (
FILTER ( Summary, [02200 Complete] && [Database Not Complete] ),
"ProjectName", 'Project Tasks'[ProjectName]
)
So for each ProjectName group, you check if that subtable has any rows that meet the specified conditions. Then you can filter for ones that meet both and pull out just the one column you want.
Here's what the Summary table looks like before it's filtered and stripped to one column:
-----------------------------------------------------------------
| ProjectName | 0200 Complete | Database Not Complete |
+-----------------+------------------+--------------------------+
| Project1 | False | True |
+-----------------+------------------+--------------------------+
| Project2 | False | True |
+-----------------+------------------+--------------------------+
| Project3 | True | True |
+-----------------+------------------+--------------------------+
| Project4 | True | True |
-----------------------------------------------------------------

Remove line breaks using linux command

My database log file looks like this...
vi test.txt
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076898 ]' LOG: SELECT nspname FROM pg_namespace ORDER BY nspname
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076899 ]' LOG: SET search_path TO "public"
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076900 ]' LOG: SELECT typname
FROM pg_type
WHERE typnamespace = (SELECT oid FROM pg_namespace WHERE nspname = current_schema())
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076897 ]' LOG: SELECT datname FROM pg_database ORDER BY datname
Because of line breaks like '\n' and '\r' I am not able to check the complete query. For e.g.
# grep '2020' test.txt
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076898 ]' LOG: SELECT nspname FROM pg_namespace ORDER BY nspname
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076899 ]' LOG: SET search_path TO "public"
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076900 ]' LOG: SELECT typname
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076897 ]' LOG: SELECT datname FROM pg_database ORDER BY datname
As you can see, the line "FROM pg_type" is missing in the above output. How do I remove line breaks in this text file? I will need to keep line break before '2020' since that is another query.
How do I write a regular expression that will remove all breaks between "LOG:" and "'2020-"
A bit of a dirty solution, but you could do something like:
cat my_log_file.log | tr '\n' ' ' | sed "s/\('[0-9]\{4\}\)/\r\n\1/g"
# OR, simpler version:
tr '\n' ' ' < my_log_file.log | sed "s/\('[0-9]\{4\}\)/\r\n\1/g"
basically, you delete all '\n', and then you add them again where they should be
$ awk '{printf "%s%s", (/^\047/ ? ors : ofs), $0; ors=ORS; ofs=OFS} END{printf "%s", ors}' file
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076898 ]' LOG: SELECT nspname FROM pg_namespace ORDER BY nspname
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076899 ]' LOG: SET search_path TO "public"
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076900 ]' LOG: SELECT typname FROM pg_type WHERE typnamespace = (SELECT oid FROM pg_namespace WHERE nspname = current_schema())
'2020-03-27T08:00:24Z UTC [ db=xdb user=root pid=9037 userid=100 xid=36076897 ]' LOG: SELECT datname FROM pg_database ORDER BY datname
awk 'match($0, r) && NR>1 {print ""}
{printf "%s", $0} END {print ""}
' r="^'2020" test.txt
This might work for you (GNU sed):
sed '/^'\''2020/{:a;N;/^\('\''2020\).*\n\1/!s/\n/ /;ta;P;D}' file
If a line begins '2020, append the next line and if that line does not begin '2020, replace the newline between the lines with a space, append the next line and repeat. Otherwise print/delete the first line and repeat.
The OP has expressed How do I write a regular expression that will remove all breaks between "LOG:" and "'2020-".To handle any year, use:
sed '/^'\''[1-9][0-9][0-9][0-9]/{:a;N;/^'\''[1-9][0-9][0-9][0-9].*\n'\''[1-9][0-9][0-9][0-9]/!s/\n/ /;ta;P;D}' file

strptime not converting character to time properly. Do I need a regexp first?

I am trying to get a series of character data converted to date time. strptime() is turnning everying into NAs.
Here is a bit of the data from a data.table:
head(dt$time2)
[1] "4/3/2012 16:00" "4/3/2012 17:00" "4/3/2012 18:00" "4/3/2012 19:00" "4/3/2012 20:00"
[6] "4/3/2012 21:00"
WHen I try and convert with:
dt[,time2:=as.POSIXct(strptime(dt$time2, "%m/%d/%y %H:%M:%S"))]
I end up with NA for all of dt$time2. If I leave off the %S, since that information is missing from the character data, i get an error about a Coerced 'list' RHS to 'character'.
Any suggestions of how to approach this would be most appreciated.
You need %Y not %y.
> dt <- c("4/3/2012 16:00", "4/3/2012 17:00", "4/3/2012 18:00","4/3/2012 19:00", "4/3/2012 20:00","4/3/2012 21:00")
> dt_t <- as.POSIXct(strptime(dt,"%m/%d/%Y %H:%M"))
> dt_t
[1] "2012-04-03 16:00:00 CDT" "2012-04-03 17:00:00 CDT" "2012-04-03 18:00:00 CDT" "2012-04-03 19:00:00 CDT"
[5] "2012-04-03 20:00:00 CDT" "2012-04-03 21:00:00 CDT"

Delete all lines between two patterns (exclusive of the pattern) using sed or awk

I have a somewhat large output text file where I need to delete all lines between two patterns but retain the pattern match.
The files look vaguely like the following output.
TEST #1
coef1 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef2 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
indicator |
0 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849
1 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361
|
year |
2 | -.4324005 2.231387 -0.19 0.847 -4.836829 3.972028
3 | -.362762 1.97184 -0.18 0.854 -4.254882 3.529358
|
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
TEST #2
coef2 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef3 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
year |
4 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849
5 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361
|
idnumber |
6 | -.4324005 2.231387 -0.19 0.847 -4.836829 3.972028
7 | -.362762 1.97184 -0.18 0.854 -4.254882 3.529358
|
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
I need to take the following output and delete all the lines between "year" and "_cons" but I need to retain the line starting with "_cons". The desired output is like so:
TEST #1
coef1 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef2 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
indicator |
0 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849
1 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
TEST #2
coef2 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef3 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
I wrote the following script (under OS X):
sed '/^ +year/,/^ +_cons/{/^ +year/!{/^ +_cons/!d}}' input.txt >output.txt
but I got the following error:
sed: 1: "/^ +year/,/^ +_cons/{/^ ...": extra characters at the end of d command
I'm not sure if this approach is even correct because I can't seem to get sed to execute. Is sed even appropriate here or should I use awk?
One last note, I need this script to work on a relatively generic Unix install. I have to send this to someone who must execute it under a very basic AIX (I think) install. No perl, no python, and I can't do much troubleshooting on their install over email.
This should work -
awk '/year/{print; getline; while($0!~/_cons/) {getline}}1' INPUT_FILE
or
awk '/_cons/{print;f=0;next}/year/{f=1;print;next}f{next}1' INPUT_FILE
Following is the Output with your input-data file:
[jaypal:~/Temp] awk '/year/{print; getline; while($0!~/_cons/) {getline}}1' file
TEST #1
coef1 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef2 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
indicator |
0 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849
1 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
TEST #2
coef2 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef3 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
Test2:
[jaypal:~/Temp] awk '/_cons/{print;f=0;next}/year/{f=1;print;next}f{next}1' file
TEST #1
coef1 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef2 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
indicator |
0 | .6647992 2.646627 0.25 0.802 -4.55925 5.888849
1 | 2.118701 5.225777 0.41 0.686 -8.19621 12.43361
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
TEST #2
coef2 | 48.36895 3.32013 14.57 0.000 41.86141 54.87649
coef3 | -50.08894 10.47335 -4.78 0.000 -70.61697 -29.56092
|
year |
_cons | 16.95753 6.342342 2.67 0.008 4.526383 29.38869
Try adding a semicolon after d to indicate that the command has ended. (GNU sed — the only sed I have handy to test with — doesn't require this, but maybe another sed would?)
Also, if you need to support multiple implementations of sed, then you can't use + to mean "one or more": it's not standard, and not all implementations support it. You can use \{1,\}, but that's pretty ugly . . . I'd just use * and tack on an extra copy.
So:
sed '/^ * year/,/^ * _cons/{/^ * year/!{/^ * _cons/!d;}}' input.txt >output.txt
(Tested, but only using GNU sed, not OS X, and certainly not AIX, sorry.)
This might work for you:
sed '/year/,/_cons/{//!d}' file
or:
awk '/_cons/{p=0};!p;/year/{p=1}' file
You can do it visually.
Just open the file with gVim, and run the command:
:g/^\s*year/+1,/^\s*_cons/-1 d
Explanation:
g global command
/^\s*year/+1 match line bellow year
/^\s*_cons/-1 match line above _cons
d delete the range
To summarize and generalize the two GNU sed solutions that work:
sed '/BEGIN/,/END/{/BEGIN/!{/END/!d;}}' input.txt
sed '/BEGIN/,/END/{//!d}' input.txt