Extracting text for a line using bash
I am completely new to linux and bash.
Terminal shows a number of lines as the output of the 'xset q'. I would like to extract the word after the first word 'timeout:' on a line from the result of 'xset q'. I guess using 'if' could be a starting point. But this needs a lot more processing. Could someone help? |
Look into grep and awk
|
Quote:
|
That's great and simple.Exactly what I was looking for.
Next, the if statement syntax looks complicated. What I would like to do is: if (xset q | grep timeout | awk -F ' ' '{print $2}') is not zero (0), then execute 'xset s off' This should be easy if the syntax is right: if [[xset q | grep timeout | awk -F ' ' '{print $2}' != "0"]] then xset s off fi Saved above in a file trial.sh Tried to run it in terminal with the following error: /mnt/Data/Temp/trial1.sh: line 3: [[xset: command not found awk: fatal: cannot open file `=' for reading: No such file or directory Edit: The batch file from the other thread is too complicated to start with. |
bash has some unique syntax requirements, spaces in certain locations do matter as well as where you place keywords. You can check your syntax by copying your script to https://www.shellcheck.net/
This isn't the only way... Code:
if [[ $(xset q | grep timeout | awk -F ' ' '{print $2}') != "0" ]] |
awk is more than capable; the pipe via grep is superfluous - and somewhat surprising it was suggested.
Strictly speaking bash is also capable without the external calls at all. Ahhh - I see michaelk got in first with suggested code. |
I know grep is superfluous, it just happened...
something like Code:
xset q | awk -F ' ' '$1 ~ /timeout/ {print $2}' |
I entered your suggestion into ShellCheck and provided the following:
$ shellcheck myscript No issues detected! I entered it into my .sh file and worked. I see what you mean by the correct placing of spaces. I wouldn't mind trying the simpler version. Edit: Didn't see your your last post michaelk. Tried it and also works. |
Code:
#!/bin/bash |
That looks great.
What I don't follow is the "$1 ~ /timeout/"? I cant see that in the ss64.com about awk. |
Basically awk searches the first column ($1) for the value timeout based on using the space as a column separator (-F ' '). Given the desired output of "timeout: 0 cycle: 0" the variables are assigned as $0 being the entire string, $1 as timeout:, $2 as 0, $3 cycle: and $4 as 0.
|
Thank you. I thought that was the case but ss64.com shows an example:
So an AWK program to retrieve Audrey's phone number is: '$1 == "Audrey" {print $2}' So, I didn't understand and still don't $1 ~ /timeout/ with the "~" and forward slashes. I did try the SS64.com example and doesn't work properly. |
For an exact match
Code:
xset q | awk -F ' ' '$1=="timeout:" {print $2}' |
Now, that does work.
The change I see is the addition of the ":" at the end of the timeout. Maybe because the contents of $1 is actually timeout:. This is what I showed originally. I must admit I didn't understand dropping it for the /timeout/ version. I still don't know the meaning of the "~" and the forward slashes. Maybe the "~" means contains timeout - don't know but it works. |
Code:
xset q | awk '/timeout/ { exit $2 }' || xset s off Code:
awk <search pattern> { command } |
All times are GMT -5. The time now is 12:43 AM. |