[SOLVED]: awk output as a variable

Need help with C, C++, perl, python, etc?

[SOLVED]: awk output as a variable

Postby cds60601 » 2018-04-08 17:16

Hi all -

Please have a look at the syntax I currently use below:

Code: Select all
#!/bin/bash
os=`hostnamectl |grep "Operating System:" |awk '{print $3" "$5}'`


When ran, it produces the output, Debian 9 What I need it to do, is put print $3 into a variable that can be used in the variable $destdir
Any help would be ideal

Cheers
Chris
Last edited by cds60601 on 2018-04-08 22:42, edited 2 times in total.
Yeah, 220, 221. Whatever it takes.
Server: Debian 9 (Stretch) Workstation: Archlinux
User avatar
cds60601
 
Posts: 136
Joined: 2017-11-25 05:58

Re: awk output as a variable

Postby peter_irich » 2018-04-08 19:20

Try it:
Code: Select all
#!/bin/bash
os=`hostnamectl |grep "Operating System:" |awk '{printf("%s %s",$3, $5)}'`
echo "$os"
peter_irich
 
Posts: 1230
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation

Re: awk output as a variable

Postby cds60601 » 2018-04-08 19:28

Thank you. This does work pretty darned well for my overall use.
The next part of the puzzle, would be to take one piece of that data (for example just the value of $3 and assign just that value to a var?

Again, thank you for the response - its certainly neater than the kludge I came up with since this posting.

Cheers
Chris
Yeah, 220, 221. Whatever it takes.
Server: Debian 9 (Stretch) Workstation: Archlinux
User avatar
cds60601
 
Posts: 136
Joined: 2017-11-25 05:58

Re: awk output as a variable

Postby Dai_trying » 2018-04-08 20:19

You could get the two items in there own variables and join them if and when required
Code: Select all
#!/bin/bash
ver=`hostnamectl |grep "Operating System:" |awk '{print $5}'`
os=`hostnamectl |grep "Operating System:" |awk '{print $3}'`
osver="$os $ver"
echo "$os"
echo "$ver"
echo "$osver"
Dai_trying
 
Posts: 531
Joined: 2016-01-07 12:25

Re: awk output as a variable

Postby cds60601 » 2018-04-08 20:24

I appreciate the resonances, thank you so much!

I was working on this and kludged something similar - not as clean as these provided examples.
The end result (so far) was to have the code span only one or two lines several lines opposed several as the examples show above.
My original intention was to derive all vars from the base line as defined in os=
I don't know how complex this can/should be but the above examples will work well enough for my needs.

Again, I thank you both for the super fast replies. I'll mark this resolved but others are welcome to continue to chime in.

Cheers
Chris
Yeah, 220, 221. Whatever it takes.
Server: Debian 9 (Stretch) Workstation: Archlinux
User avatar
cds60601
 
Posts: 136
Joined: 2017-11-25 05:58


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable