LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash - read or write to specific line in text file? (http://www.linuxquestions.org/questions/programming-9/bash-read-or-write-to-specific-line-in-text-file-664654/)

babag 08-22-2008 02:35 PM

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

Uncle_Theodore 08-22-2008 02:49 PM

Quote:

Originally Posted by babag (Post 3256264)
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.
Well, for example, you can do it like this

sed -n '3p' truefalse.txt

prints the third line.

babag 08-22-2008 03:21 PM

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
  do something
else
  do something else

what would be the syntax for something like that?

thanks again,
BabaG

ta0kira 08-22-2008 03:55 PM

If you don't care what line 5 is, use this to unconditionally replace the line:
Code:

sed '5 ctrue'
ta0kira

babag 08-23-2008 12:55 PM

i can get this to print out the third line:

Code:

sed -n '3p' truefalse.txt
but how do i get that third line into a variable?

tried:

Code:

variable= sed -n '3p' truefalse.txt
echo $variable

but that didn't do it.

thanks,
BabaG

David the H. 08-23-2008 01:04 PM

To get the output of a command, put it inside "$()".

Code:

variable=$(sed -n '3p' truefalse.txt)
echo $variable


babag 08-23-2008 01:12 PM

thanks david the h.

this also seems to work (search is a good thing):

Code:

export variable=`sed -n '3p' truefalse.txt`
is there any advantage of one solution over the other?

thanks again,
BabaG

David the H. 08-23-2008 01:13 PM

By the way, you can also use the same pattern directly in an if statement:

Code:

if [ "$(sed -n '3p' truefalse.txt)" = "true" ]; then
  do something
else
  do something else
fi

There are several good tutorials that explain the syntax details of the various scripting functions, such as http://www.linuxcommand.org

David the H. 08-23-2008 01:18 PM

Quote:

Originally Posted by babag (Post 3257146)
Code:

export variable=`sed -n '3p' truefalse.txt`
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.

babag 08-23-2008 01:32 PM

trying this but getting an error for unexpected 'else':
Code:

variable=$(sed -n '3p' truefalse.txt)

echo $variable

if [ "$variable" = "True" ] then
    sed '3 cFalse' truefalse.txt
else
    sed '3 cTrue' truefalse.txt
fi

variable=$(sed -n '3p' truefalse.txt)

echo $variable


babag 08-23-2008 02:23 PM

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
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:
Code:

sed -i '3 cFalse' truefalse.txt
thanks everyone,
BabaG

David the H. 08-23-2008 02:44 PM

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:44 PM.