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: true false true true true false false false false false false false false false false false 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. thanks, BabaG |
Quote:
sed -i '5s/true/false/' truefalse.txt Quote:
sed -n '3p' truefalse.txt prints the third line. |
thanks uncle theodore!
so, how would i turn : sed -n '3p' truefalse.txt into something useful, like: Code:
if sed -n '3p' truefalse.txt = true then thanks again, BabaG |
If you don't care what line 5 is, use this to unconditionally replace the line:
Code:
sed '5 ctrue' |
i can get this to print out the third line:
Code:
sed -n '3p' truefalse.txt tried: Code:
variable= sed -n '3p' truefalse.txt thanks, BabaG |
To get the output of a command, put it inside "$()".
Code:
variable=$(sed -n '3p' truefalse.txt) |
thanks david the h.
this also seems to work (search is a good thing): Code:
export variable=`sed -n '3p' truefalse.txt` thanks again, BabaG |
By the way, you can also use the same pattern directly in an if statement:
Code:
if [ "$(sed -n '3p' truefalse.txt)" = "true" ]; then |
Quote:
|
trying this but getting an error for unexpected 'else':
Code:
variable=$(sed -n '3p' truefalse.txt) |
ok. two things.
first, found the problem with the unexpected 'else'. i'd left out a ; between the ']' and 'then'. Code:
if [ "$variable" = "True" ] ; then 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: Code:
sed -i '3 cFalse' truefalse.txt BabaG |
Whoops! I missed that in my post too; so if you got it from me, I'm sorry. I've gone back and fixed it in my post.
PS: Don't forget that the test is case-sensitive. |
All times are GMT -5. The time now is 12:04 PM. |