Score:0

Variable in AWK

cn flag

How can I add a variable within awk command? below is my command:

cat /home/ubuntu/test/copy.txt | awk 'NR==21{print ".host = "$line";"}1'

$line is basically an IP address which is retrieved from a text file. With the above command the output looks like

.host = ;

Any immediate help would be really appreciated.

Format of copy.txt

backend default {
#.host = value; need to be here
.port = "8080";
}
Romeo Ninov avatar
in flag
do you want to get `line` variable from file or from other source?
Romeo Ninov avatar
in flag
And what is the exact format of the `copy.txt` file?
serverstackqns avatar
cn flag
@RomeoNinov- please check my update. also, regarding line, I am already capturing it from a file using while loop.
Score:0
in flag

The variables in awk can be transferred from shell on this way:

awk -v variable=$line '.....

and used inside on this way:

awk -v line1=$line 'NR==21 {print ".host = "line1";"}1'

The variable should be set beforehand in bash so the things become:

line=192.168.0.10
awk -v line1=$line 'NR==21 {print ".host = "line1";"}1' /path/to/file ...

Also you do not need cat, just use this way:

awk '....' /home/ubuntu/test/copy.txt

To store the output in a file you should use something like:

awk '....' /home/ubuntu/test/copy.txt >/home/ubuntu/test/new_copy.txt

and the result will be stored in /home/ubuntu/test/new_copy.txt

serverstackqns avatar
cn flag
It works, however the file doesnt seem to be saved. The reason is I am able to see the file with the update printed, but checking the file in another window, there's no content in line 21. Any idea why?
Romeo Ninov avatar
in flag
Because `awk` only print the content. Let me add more explanations to the answer
serverstackqns avatar
cn flag
Thanks, it works, but in line1 should be updated with the value along with "" (double quotes) example: "192.168.0.10". Now it is coming as 192.168.10.0. How can I achieve that?
Romeo Ninov avatar
in flag
@serverstackqns, before exec the `awk` you should run command like `line=192.168.0.10` and in `awk` variable `line1` will get this value
serverstackqns avatar
cn flag
If you don't mind, can you please update the answer with this too.
Romeo Ninov avatar
in flag
@serverstackqns, done. Maybe you should spend some time to learn `bash` scripting
serverstackqns avatar
cn flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/140551/discussion-between-serverstackqns-and-romeo-ninov).
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.