LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Extracting text for a line using bash (https://www.linuxquestions.org/questions/linux-newbie-8/extracting-text-for-a-line-using-bash-4175721894/)

fhutt 02-10-2023 06:12 PM

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?

jmgibson1981 02-10-2023 06:28 PM

Look into grep and awk

michaelk 02-10-2023 07:02 PM

Quote:

xset q | grep timeout

timeout: 0 cycle: 0

xset q | grep timeout | awk -F ' ' '{print $2}'
0
As a hint. Not exactly sure how you define word but the awk command above splits the results by space and {print $2} outputs the second field. From your other thread if you post the batch file you wish to convert we can provide better help.

fhutt 02-10-2023 07:35 PM

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.

michaelk 02-10-2023 07:53 PM

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" ]]
then
  xset s off
fi


syg00 02-10-2023 07:57 PM

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.

michaelk 02-10-2023 08:01 PM

I know grep is superfluous, it just happened...
something like
Code:

xset q | awk -F ' ' '$1 ~ /timeout/ {print $2}'

fhutt 02-10-2023 08:20 PM

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.

michaelk 02-10-2023 08:32 PM

Code:

#!/bin/bash
[ "$(xset q | awk -F ' ' '$1 ~ /timeout/ {print $2}')" -eq "0" ] || xset s off

syg00 is much better at scripting then I.

fhutt 02-10-2023 08:39 PM

That looks great.
What I don't follow is the "$1 ~ /timeout/"?
I cant see that in the ss64.com about awk.

michaelk 02-10-2023 08:47 PM

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.

fhutt 02-10-2023 09:05 PM

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.

michaelk 02-10-2023 09:14 PM

For an exact match
Code:

xset q | awk -F ' ' '$1=="timeout:" {print $2}'
The ~ is a regular expression that basically means match column that starts with timeout

fhutt 02-10-2023 09:27 PM

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.

pan64 02-11-2023 01:18 AM

Code:

xset q | awk '/timeout/ { exit $2 }' || xset s off
Code:

awk <search pattern> { command }
/timeout/ means looking for the text timeout
$1=="timeout:" means the first field is equal to timeout:
~ is a matching operator but I think you can forget it now.

see man awk about how does it work or here: https://www.gnu.org/software/gawk/ma...exp-Usage.html


All times are GMT -5. The time now is 12:43 AM.