LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to use a variable in sed for adding the text into the file? (https://www.linuxquestions.org/questions/programming-9/how-to-use-a-variable-in-sed-for-adding-the-text-into-the-file-4175643901/)

BW-userx 12-08-2018 01:37 PM

How to use a variable in sed for adding the text into the file?
 
Code:

  if [ -x /usr/bin/ifconfig ] || [ -x /sbin/ifconfig ] ; then
                device="$(ifconfig | awk 'NR==10 {print $1}' | rev | cut -c 2- | rev)"
                NIC="device=\"$device;\""
                echo "$NIC"
                none of these work
                sed -i '100i\$NIC' epplets/net.c
                sed -i '100i\"$NIC"' epplets/net.c
                sed -i "100i\$NIC" epplets/net.c
        fi

the code getting the NIC var value is good, device="wlo1;"

Now how do I use that to add it into the file? It keeps just adding the word
$NIC
in the file and not its value
Code:

net_get_bytes_inout(const char *device, double *in_bytes, double *out_bytes)
{
$NIC


scasey 12-08-2018 01:55 PM

I'm probably missing something, but wouldn't
Code:

echo $NIC >> epplets/net.c
append the value in $NIC to the file?

Was there some specific reason you wanted to use sed?

BW-userx 12-08-2018 02:02 PM

Quote:

Originally Posted by scasey (Post 5934892)
I'm probably missing something, but wouldn't
Code:

echo $NIC >> epplets/net.c
append the value in $NIC to the file?

it has to be at line 100 not appended to it on the bottom of the file. Other wise if the variable is in the wrong place, being at the end of the file, it will not compile properly nor over write what the variable is set to in the code so it will work with wifi and not eth0.
Quote:

Was there some specific reason you wanted to use sed?
only because it is a tool that is used to replace, and add text into files, and I searched around some more looking to see what awk can do, and found this.
Code:

awk -v x="$NIC" 'NR==100{print x} 1' epplets/net.c > tmp && mv tmp epplets/net.c
it worked so I am marking this solved.

thanks for your inquiry.

syg00 12-08-2018 04:04 PM

Code:

sed -i "100i$NIC" epplets/net.c
Double quotes, no backslash.
You could use the backslash form if you really wanted, but I wouldn't recommend it in a script. Read the doco more closely for whay.

BW-userx 12-08-2018 04:15 PM

Quote:

Originally Posted by syg00 (Post 5934918)
Code:

sed -i "100i$NIC" epplets/net.c
Double quotes, no backslash.
You could use the backslash form if you really wanted, but I wouldn't recommend it in a script. Read the doco more closely for whay.

are you shure?
Code:

userx@SlackOLatern.net:/media/projects/git-eterm/e16-epplets-0.16/epplets
$ NIC=HoYO

$ sed -i "102i$NIC" net.c
sed: -e expression #1, char 4: expected \ after `a', `c' or `i'

adding the \ again it just
Code:

unsigned char
net_get_bytes_inout(const char *device, double *in_bytes, double *out_bytes)
{
//fix so it will not keep sayig what it is not
device = "wlo1";
$NIC

uses the var name not value, anyways awk does it just fine.

syg00 12-08-2018 04:18 PM

Arggghh - I always use GNU sed. :doh:

BW-userx 12-08-2018 05:09 PM

Quote:

Originally Posted by syg00 (Post 5934923)
Arggghh - I always use GNU sed. :doh:

I always use whatever is installed. :D

it's Linux, one would then think it is GNU.. hummm, let me look.

Code:

$ sed --version
sed (GNU sed) 4.5
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.


l0f4r0 12-10-2018 04:58 AM

Quote:

Originally Posted by BW-userx (Post 5934922)
are you shure?
Code:

userx@SlackOLatern.net:/media/projects/git-eterm/e16-epplets-0.16/epplets
$ NIC=HoYO

$ sed -i "102i$NIC" net.c
sed: -e expression #1, char 4: expected \ after `a', `c' or `i'

adding the \ again it just
Code:

unsigned char
net_get_bytes_inout(const char *device, double *in_bytes, double *out_bytes)
{
//fix so it will not keep sayig what it is not
device = "wlo1";
$NIC

uses the var name not value, anyways awk does it just fine.

Then try the following:
Code:

sed -i "100i\\$NIC" epplets/net.c


All times are GMT -5. The time now is 06:31 AM.