bash - read or write to specific line in text file?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash - read or write to specific line in text file?
i have a text file that is to be modified by one script
and read by another. the text file is simple and short,
consisting of 16 true/false lines as follows:
how do i tell the first script to write to a specific line
in the textfile to modify it.
for example, line five is currently 'true' in the above
listing. how would i tell the script to overwrite that to
false?
the second script will be running a loop. depending on
how many times the loop has run, the script needs to
read a different line in the text file. how do i specify
to only read, say, line five of the above example?
the file is short enough that i probably could read the
whole thing into an array as i've been shown previousy
how to do that, but i thought this might be more efficient
and good to learn.
for example, line five is currently 'true' in the above
listing. how would i tell the script to overwrite that to
false?
Like this, for example
sed -i '5s/true/false/' truefalse.txt
Quote:
the second script will be running a loop. depending on
how many times the loop has run, the script needs to
read a different line in the text file. how do i specify
to only read, say, line five of the above example?
the file is short enough that i probably could read the
whole thing into an array as i've been shown previousy
how to do that, but i thought this might be more efficient
and good to learn.
is there any advantage of one solution over the other?
thanks again,
BabaG
They both do the same thing, but most people find the $() syntax more readable and less mistake-prone, so it's usually recommended. I seem to recall that there may also be a few unusual situations where the backticks won't work, but I don't know enough about scripting myself to say for sure.
first, found the problem with the unexpected 'else'.
i'd left out a ; between the ']' and 'then'.
Code:
if [ "$variable" = "True" ] ; then
thanks again david the h for the link. that's where i
found the missing ;.
secondly, i couldn't get either of the examples above
to replace the text in the truefalse.txt document. i
did, however, by playing, find that a sort of hybrid
did make this all work:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.