I'm trying to check if time clock is synchronized via bash script.
So first i have executed the command timedatectl which outputs the following:
Local time: Mi 2021-03-17 12:52:53 CET
Universal time: Mi 2021-03-17 11:52:53 UTC
RTC time: Mi 2021-03-17 11:52:53
Time zone: Europe/Berlin (CET, +0100)
System clock synchronized: yes
systemd-timesyncd.service active: yes
RTC in local TZ: no
So the goal is to get the value of System clock synchronized which is yes and assign it to a variable and print it. therefore i did the follwing:
1-# check if time synchronized
2-syncTime="$(timedatectl | grep 'System clock')" # returns the whole line
3-echo "$syncTime"# make sure the line is saved
4-ifTimeSynched="$( $syncTime | grep -oP 'synchronized: \K\w+')" # this supposes to save the word
after synchronized:
echo "$ifTimeSynched"
Line 4 should get the word next to the word synchronized: which is in this case the yesHowever, when i print $ifTimeSynched as shown above, it returns and empty line.
Any reason why it doesn't catch the word yes?
thank in advance
This could be easily done with using awk. You need to search for string System clock synchronized in output of your command timedatectl(which is passed as an input to awk command) and print desired column(field) which is last field in your case.
ifTimeSynched=$(timedatectl | awk '/System clock synchronized/{print $NF}')
echo "$ifTimeSynched"
try:
ifTimeSynched=$( echo $syncTime | grep -oP 'synchronized: \K\w+')
Related
A have daily syslog files which contain syslog messages in the format: MMM DD HH:MM:SS additional_data_here
We make a change, then want to see if the syslog messages continue.
For example, say a change was made at 09:55. I want to ignore everything prior to the first line that contains Oct 29 09:55:00. Then I want to grep for my error message after that first line match.
For this example, I have to create several different statements, like this:
grep -e "Oct 29 09:5[5-9]" syslog20211029 | grep "[my message]"
grep -e "Oct 29 1[0-1]:" syslog20211029 | grep "[my message]"
But I do this often enough that I'd like to find a better, more consistent way. Something like:
start-at-first-match "Oct 29 09:55:00" syslog20211029 | grep "[my message]"
But I don't know what the start-at-first-match option is. Any suggestions?
If you want to restrict yourself to using grep, you can't really but with the option -A num it can still meet your need (giving a big number for num) :
grep -A 10000000 "Oct 29 09:55:00" syslog20211029
This will print the matching line and the next 10 million.
If you want everything that follows the line for sure (without having to give an "unreachable" number of lines), you have to use another command (like sed or awk). Using sed: sed -n '/Oct 29 09:55:00/,$ p' (with -n won't print the lines by default, and from the line you want, between /pattern/, to the end of file $ you ask sed to print the lines).
I have the following issue.
In a script, I have to execute the hdparm command on /dev/xvda1 path.
From the command output, I have to extract the MB/sec values calculated.
So, for example, if executing the command I have this output:
/dev/xvda1:
Timing cached reads: 15900 MB in 1.99 seconds = 7986.93 MB/sec
Timing buffered disk reads: 478 MB in 3.00 seconds = 159.09 MB/sec
I have to extract 7986.93 and 159.09.
I tried:
grep -o -E '[0-9]+', but it returns to me all the six number in the output
grep -o -E '[0-9]', but it return to me only the first character of the six values.
grep -o -E '[0-9]+$', but the output is empty, I suppose because the number is not the last character set of outoput.
How can I achieve my purpose?
To get the last number, you can add a .* in front, that will match as much as possible, eating away all the other numbers. However, to exclude that part from the output, you need GNU grep or pcregrep or sed.
grep -Po '.* \K[0-9.]+'
Or
sed -En 's/.* ([0-9.]+).*/\1/p'
Consider using awk to just print the fields you want rather than matching on numbers. This will work using any awk in any shell on every Unix box:
$ hdparm whatever | awk 'NF>1{print $(NF-1)}'
7986.93
159.09
I was doing some adb shell stuff on windows and stuck at a point. Here's what I was doing..
I was printing all installed apps on my phone and getting their exact path.
zeroltetmo:/ # pm list packages -f
package:/system/app/FilterProvider/FilterProvider.apk=com.samsung.android.provider.filterprovider
package:/system/priv-app/CtsShimPrivPrebuilt/CtsShimPrivPrebuilt.apk=com.android.cts.priv.ctsshim
package:/system/app/YouTube/Youtube.apk=com.google.android.youtube
package:/system/app/vsimservice/vsimservice.apk=com.sec.vsimservice
package:/system/priv-app/WallpaperCropper/WallpaperCropper.apk=com.android.wallpapercropper
package:/system/framework/framework-res.apk=android
package:/system/framework/samsung-framework-res/samsung-framework-res.apk=com.samsung.android.framework.res
package:/data/app/com.whatsapp-1/base.apk=com.whatsapp
package:/data/app/ru.meefik.busybox-2/base.apk=ru.meefik.busybox
package:/data/app/com.google.android.play.games-1/base.apk=com.google.android.play.games
But,
I want this to print only system/app directory but only upto folder name instead of the full path. What i'm doing is piping this to grep and using this pattern to get the result.
zeroltetmo:/ # pm list packages -f | grep -o "system/app.*\/"
system/app/FilterProvider/
system/app/RootPA/
system/app/YouTube/
system/app/ClipboardSaveService/
system/app/TetheringAutomation/
system/app/GoogleExtShared/
system/app/WfdBroker/
system/app/vsimservice/
system/app/USBSettings/
system/app/EasyOneHand3/
But the problem is this / at the end of folder name that I'm stuck with.
You can filter the trailing slashes out with sed like that:
pm list packages -f | grep -o "system/app.*/" | sed 's,/$,,'
Explanation of the sed command:
s stands for substitution
, delimits command name from its arguments - it's easier to use something different / when we want to replace /
/$ - string to be replaced. In this case it means slash at the end of the line
The string to replace /$ with is empty because we want to remove it.
I am by no means a programmer. I'll put that right out there. However, I'm trying to write a script that writes fstab entries after grabbing uuid data. This is in a OpenWRT environment on my router. My goal:
Grab uuid info using awk, like:
blkid /dev/sdb2 | awk -F'UUID="|"' '{print $2}'
Send the full uuid output to the end of this command:
uci set fstab.#mount[-1].uuid=
Execute that command with the correct uuid.
This command writes that uuid to the correct place in fstab. How can this be accomplished in a bash-script?
Thanks,
KG
You can either execute the command from within awk (using system(...)), or from within the shell (using uci set fstab.#mount[-1].uuid=$(blkid ...)).
I can't test it with the uci command but I believe this should do what you need.
bblkid /dev/sda1 | uci set fstab.#mount[-1].uuid=$( awk -F'UUID="|"' '{print $2}' )
Edit: this would probably be better.
uci set fstab.#mount[-1].uuid=$( bblkid /dev/sda1 | awk -F'UUID="|"' '{print $2}' )
Edit 2: little bit shorter but should get the same result. Promise this is the last one
uci set fstab.#mount[-1].uuid=$( bblkid /dev/sda1 | tr -d UUID=\" )
The above options didn't work quite right.
What if I have this in fstab:
option uuid 'hotdog'
I want to replace the word "hotdog" with the output of the uuid from this command:
On my router that output looks like:
root#OpenWrt:/etc/config# blkid /dev/sdb2 | awk -F'UUID="|"' '{print $2}'
8618b8fe-93a9-488c-a901-0df30898c82e
or
root#OpenWrt:/etc/config# block info | grep sdb2
/dev/sdb2: UUID="8618b8fe-93a9-488c-a901-0df30898c82e" NAME="EXT_JOURNAL" VERSION="1.0" TYPE="ext4"
I want to then replace the word hotdog with the random uuid, and keep the semi-colons. Keep in mind that the uuid will change each time I format that partition.
I am passing all my svn commit log messages to a file and want to grep only the JIRA issue numbers from that.
Some lines might have more than 1 issue number, but I want to grab only the first occurrence.
The pattern is XXXX-999 (number of alpha and numeric char is not constant)
Also, I don't want the entire line to be displayed, just the JIRA number, without duplicates. I use the following command but it didn't work.
Could someone help please?
cat /tmp/jira.txt | grep '^[A-Z]+[-]+[0-9]'
Log file sample
------------------------------------------------------------------------
r62086 | userx | 2015-05-12 11:12:52 -0600 (Tue, 12 May 2015) | 1 line
Changed paths:
M /projects/trunk/gradle.properties
ABC-1000 This is a sample commit message
------------------------------------------------------------------------
r62084 | usery | 2015-05-12 11:12:12 -0600 (Tue, 12 May 2015) | 1 line
Changed paths:
M /projects/training/package.jar
EFG-1001 Test commit
Output expected:
ABC-1000
EFG-1001
First of all, it seems like you have the second + in the wrong place, it should be at the end of [0-9] expression.
Second, I think all you need to do this is use the -o option to grep (to display only the matching portion of the line), then pipe the grep output through sort -u, like this:
cat /tmp/jira.txt | grep -oE '^[A-Z]+-[0-9]+' | sort -u
Although if it were me, I'd skip the cat step and just give the filename to grep, as so:
grep -oE '^[A-Z]+-[0-9]+' /tmp/jira.txt | sort -u
Six of one, half a dozen of the other, really.