LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-18-2021, 05:39 AM   #1
sncak
LQ Newbie
 
Registered: Feb 2021
Posts: 13

Rep: Reputation: Disabled
Question How to use sed command to dynamically insert line number


I am trying to dynamically update sed line number using variable

and I am using the script

Code:
#!/bin/bash

FILE=$(awk 'NR==1{print $1}' log.txt)      #getting File path from log file
LINE=$(awk 'END{print}' log.txt)           #getting Line number from log file
cat $FILE > comp1
clear
sed -i "$LINEs/asm/asm-generic/" $FILE     #sed not working properly
cat $FILE > comp2
clear
echo "Done"
diff comp1 comp2

exit 0
I am getting no error but the substitution is no happening and code getting exited with no errors.

Last edited by sncak; 02-18-2021 at 05:42 AM.
 
Old 02-18-2021, 05:46 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
How do you know that this
Code:
LINE=$(awk 'END{print}' log.txt)
gets the line number? It just saves the last line of log.txt to LINE.

Output the value of LINE before the sed command to see what it actually contains:
Code:
echo "|$LINE|"
More importantly, judging from your other thread, I suspect this is an example of XY problem.

Isn't finding all the relevant places in source where asm must be changed to asm-generic easier than evaluating the build log?

Last edited by shruggy; 02-18-2021 at 06:08 AM.
 
1 members found this post helpful.
Old 02-18-2021, 06:07 AM   #3
sncak
LQ Newbie
 
Registered: Feb 2021
Posts: 13

Original Poster
Rep: Reputation: Disabled
My log.txt file looks like this

Code:
/usr/src/linux-headers-5.10.16-olimex/include/asm-generic/bitops/non-atomic.h
5
and I am setting data in first-line to FILE variable and Second to Line variable.
 
Old 02-18-2021, 06:08 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by sncak View Post
Code:
sed -i "$LINEs/asm/asm-generic/" $FILE     #sed not working properly
To be honest, I don't understand the script, but the sed line doesn't work because you use a shell variable named LINEs. Most probably, the intention is
Code:
sed -i "${LINE}s/asm/asm-generic/" $FILE     #sed working properly

Last edited by berndbausch; 02-18-2021 at 06:09 AM. Reason: typo
 
1 members found this post helpful.
Old 02-18-2021, 06:12 AM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I would use tail to retrieve the last line from log,txt. If the first line in log.txt just contains the file to be edited, I would use head for that.
 
Old 02-18-2021, 06:13 AM   #6
sncak
LQ Newbie
 
Registered: Feb 2021
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
To be honest, I don't understand the script, but the sed line doesn't work because you use a shell variable named LINEs. Most probably, the intention is
Code:
sed -i "${LINE}s/asm/asm-generic/" $FILE     #sed working properly
thanks, working fine now.
 
Old 02-18-2021, 06:15 AM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554
Quote:
Originally Posted by sncak View Post
How to use sed command to dynamically insert line number
Quote:
Originally Posted by sncak View Post
I am trying to dynamically update sed line number using variable
Those are not the same thing, and neither one matches what the code appears to be trying to do.
It's almost always a good idea to provide brief example inputs and outputs, to clarify what you're actually trying to do.


Quote:
Code:
FILE=$(awk 'NR==1{print $1}' log.txt)      #getting File path from log file
LINE=$(awk 'END{print}' log.txt)           #getting Line number from log file
What does log.txt look like?

Quote:
cat $FILE > comp1
Why not cp "$FILE" comp1 ?

Quote:
sed -i "$LINEs/asm/asm-generic/" $FILE #sed not working properly
Sed is not the problem.

What do you think the LINEs variable contains...?

Also, always wrap filename variables with quotes.



edit: ok, I'm way too slow... how did typing that take 8+ minutes? :'(


Last edited by boughtonp; 02-18-2021 at 06:18 AM.
 
Old 02-18-2021, 06:31 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
And even more importantly, the Linux sources for kernel 5.10.16 still have arch/arm/include/asm and arch/arm/include/uapi/asm. Why are you trying to change this to asm-generic? All you need is to specify the correct include path in the -I flag to compiler.

Last edited by shruggy; 02-18-2021 at 06:50 AM.
 
  


Reply

Tags
bash script, sed bash, shell script



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] insert line in file every 1000th line via sed or in vi tiny.deluxe Programming 6 01-11-2018 04:51 AM
Use SED to insert textfile before a line, using a COMBINED search wilsoncpu Linux - Newbie 2 09-27-2014 07:17 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 09:14 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:34 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