LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2018, 01:37 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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

Last edited by BW-userx; 12-08-2018 at 01:38 PM.
 
Old 12-08-2018, 01:55 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,726

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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?
 
Old 12-08-2018, 02:02 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by scasey View Post
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.

Last edited by BW-userx; 12-08-2018 at 02:08 PM.
 
Old 12-08-2018, 04:04 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
Old 12-08-2018, 04:15 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by syg00 View Post
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.

Last edited by BW-userx; 12-08-2018 at 04:19 PM.
 
Old 12-08-2018, 04:18 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Arggghh - I always use GNU sed.
 
Old 12-08-2018, 05:09 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Original Poster
Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by syg00 View Post
Arggghh - I always use GNU sed.
I always use whatever is installed.

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>.

Last edited by BW-userx; 12-08-2018 at 05:10 PM.
 
Old 12-10-2018, 04:58 AM   #8
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by BW-userx View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] ksh read variable from text file and populate with content of shell variable WindozBytes Programming 4 09-17-2012 01:48 PM
[SOLVED] Using a variable in sed to pick a specific line from a text file, bash Sunvic Linux - Newbie 10 08-12-2012 11:03 AM
[SOLVED] Bash command to 'cut' text into another text file & modifying text. velgasius Programming 4 10-17-2011 04:55 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration