Getting the linux time since power on - python-3.x

I've got
time = os.popen("tuptime").readline()
and the output should be something like:
System startups: 1 since 21:32:26 01.05.2020
System shutdowns: 0 ok - 0 bad
System uptime: 100.0% - 1 day, 16 hours, 49 minutes, 41 seconds
System downtime: 0.0% - 0 seconds
System life: 1 day, 16 hours, 49 minutes, 41 seconds
Largest uptime: 1 day, 16 hours, 49 minutes, 41 seconds from 21:32:26 01.05.2020
Shortest uptime: 1 day, 16 hours, 49 minutes, 41 seconds from 21:32:26 01.05.2020
Average uptime: 1 day, 16 hours, 49 minutes, 41 seconds
Largest downtime: 0 seconds
Shortest downtime: 0 seconds
Average downtime: 0 seconds
Current uptime: 1 day, 16 hours, 49 minutes, 41 seconds since 21:32:26 01.05.2020
how can i get and print out the "System life"?

you can use "tuptime| grep -i 'system life'" in the cmd instead of "tuptime"

Related

I want to resample the columns in a group of 7 based on weeks starting sunday and ending saturday

Cid 2021-05-01| 2021-05-02 | 2021-05-03 | 2021-05-04| 2021-05-05|2021-05-06|2021-05-07|2021-05-08
120 25 30 40 10 15 20 5 5
220 10 20 30 10 50 15 35 55
430 16 4 20 10 25 25 15 6
In the requirement Cid is the index and columns are of int64 datatype
I want to convert the columnNames to datetime and resample them in groups of 7 based(i.e) weeks, starting from sunday.
I tried converting the columnNames to dateTime format but unable to do so for past 2 day
Required Output
Cid 2021-05-01|2021-05-08
120 145 5
220 170 55
430 115 6
You can use resample to do 7 days aggregation.
# Convert columns to DatetimeIndex if it is not yet
df = df.set_index('Cid')
df.columns = pd.to_datetime(df.columns)
Then, aggregate the sum over 7 days. "W-SAT" is weekly interval starting on Saturday. You want the date from the left side of the interval, so add label and closed options to left.
df.resample('W-SAT', label='left', closed='left', axis=1).sum()
Result
2021-05-01 2021-05-08
Cid
120 145 5
220 170 55
430 115 6

resampling a pandas dataframe and filling new rows with zero

I have a time series as a dataframe. The first column is the week number, the second are values for that week. The first week (22) and the last week (48), are the lower and upper bounds of the time series. Some weeks are missing, for example, there is no week 27 and 28. I would like to resample this series such that there are no missing weeks. Where a week was inserted, I would like the corresponding value to be zero. This is my data:
week value
0 22 1
1 23 2
2 24 2
3 25 3
4 26 2
5 29 3
6 30 3
7 31 3
8 32 7
9 33 4
10 34 5
11 35 4
12 36 2
13 37 3
14 38 10
15 39 5
16 40 7
17 41 10
18 42 11
19 43 15
20 44 9
21 45 13
22 46 5
23 47 6
24 48 2
I am wondering if this can be achieved in Pandas without creating a loop from scratch. I have looked into pd.resample, but can't achieve the results I am looking for.
I would set week as index, reindex with fill_value option:
start, end = df['week'].agg(['min','max'])
df.set_index('week').reindex(np.arange(start, end+1), fill_value=0).reset_index()
Output (head):
week value
0 22 1
1 23 2
2 24 2
3 25 3
4 26 2
5 27 0
6 28 0
7 29 3
8 30 3

Python x days y hrs z mins to date

I have a pandas dataframe with the following values:
tx['Age']
0 7 mins ago
1 8 mins ago
2 12 mins ago
3 14 mins ago
4 15 mins ago
5 9 hrs 21 mins ago
6 11 hrs 13 mins ago
7 11 hrs 13 mins ago
8 11 hrs 14 mins ago
9 11 hrs 15 mins ago
10 1 day 3 hrs ago
11 1 day 3 hrs ago
12 1 day 3 hrs ago
13 1 day 3 hrs ago
14 1 day 3 hrs ago
15 1 day 3 hrs ago
16 1 day 3 hrs ago
17 1 day 3 hrs ago
18 1 day 3 hrs ago
19 1 day 3 hrs ago
20 1 day 4 hrs ago
21 2 days 12 hrs ago
22 2 days 14 hrs ago
23 2 days 14 hrs ago
24 2 days 22 hrs ago
Name: Age, dtype: object
And I would like to transform this column into dates.
Any idea folks?
Thanks!
There are two parts in your problem. First, extract the days, hours, and minutes and replace the missing parts with zeros:
pattern = r"(?:(\d+) days?)? ?(?:(\d+) hrs?)? ?(?:(\d+) mins?)?"
parts = df['Age'].str.extract(pattern).fillna(0).astype(int)
Second, convert the days, hours, and minutes to minutes and feed the minutes to the TimedeltaIndex constructor:
minutes = ((parts[0] * 24 + parts[1]) * 60 + parts[2]).astype(str)
-pd.TimedeltaIndex("00:" + minutes + ":00")
Note the "minus" sign: it means "ago." The result is a Timedelta. To make it a date, you must add it to some reference date.

Delete Row After Counting Number of Columns In Text File

I need to dynamically delete a line after counting the number of columns in it. For instance, if the number of columns in the line is less than X, delete the entire line. I have a text file that contains 100+ lines.
Here's what I have so far, which counts the total number of columns in each line of the text file...
Text Sample:
KGAI 2 2 40 50 50 98 75 10 35 40 15 25 15 55
KGED 3 3 15 25 20 60 60 20 50 25 15 25 20 40
KGFL 1 10 3 4 3 85 25
KGHG 15 20 40 20 20 20 20
KGKJ 20 80 25 20 25 97 50 20 60 45 30 30 25 30
KGNR 2 30 4 5 5 25 90 10 35 15 15 15 10 20
KGON 1 1 20 10 5 85 65 5 20 30 15 10 15 25
KGTB
KHEF 2 2 20 30 50 98 60 10 30 25 10 15 10 45
Code:
cat text_data | awk 'BEGIN{FS=" "};{print NF}'
Output:
15
15
8
8
15
15
15
1
15
Do it the other way round: just print those that have at least X columns:
awk -v cols=5 'NF>=cols' file
That is, set the variable cols to the value of minimum amount of cols you want the line to have. Whenever it is true, the line will be printed.
In this case it returns:
KGAI 2 2 40 50 50 98 75 10 35 40 15 25 15 55
KGED 3 3 15 25 20 60 60 20 50 25 15 25 20 40
KGFL 1 10 3 4 3 85 25
KGHG 15 20 40 20 20 20 20
KGKJ 20 80 25 20 25 97 50 20 60 45 30 30 25 30
KGNR 2 30 4 5 5 25 90 10 35 15 15 15 10 20
KGON 1 1 20 10 5 85 65 5 20 30 15 10 15 25
KHEF 2 2 20 30 50 98 60 10 30 25 10 15 10 45

How to delete a certain number of characters after a string in vim?

I have a large file that I need to edit a certain number of characters out of after a certain pattern. This is an example of part of my file:
#IB4UYMV03HCKRV
100 100 100 100 100 100 100 100 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 39 39 39 40 40 40 40
#IB4UYMV03GZDSU
100 100 100 100 100 100 100 100 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 39 39 39 40 40 40 40 40 40 40 40 40 40 40 39 39 39 40 40 40 40 40 40 40 40 40 "
After the 100 100 100 100 100 100 100 100 I would like to delete the next twelve 40 which is 36 characters with spaces. I tried using a find and replace but the characters are not always 40, they could be any two figure number.
Is there a way to delete 36 characters after a pattern?
If you put the cursor over the first 4 in your string of 12 40s, you can do 36x in normal mode which will delete a single character 36 times.
Or, if you've got a regular pattern, you can use the following substitution:
:%s/^\v(100 ){8}\zs(\d\d ){12}/
Broken down:
Ex Command:
% All lines
s Substitute
Pattern:
^ Anchor to start of line
\v Very magic mode, to avoid excessive slashes
(100 ){8} 8 instances of the string "100 "
\zs Start selection
(\d\d ){12} 12 instances of the string "\d\d " (i.e., \d is any digit)
Replacement:
Nothing (i.e., remove it)
The "selection" is the part that is ultimately replaced. Note that there is no matched \ze (end selection), because we don't care how the pattern ends.

Resources