LinuxQuestions.org
Visit Jeremy's Blog.
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 08-22-2008, 01:35 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 419

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

Last edited by babag; 08-22-2008 at 01:37 PM.
 
Old 08-22-2008, 01:49 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Quote:
Originally Posted by babag View Post
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.
 
Old 08-22-2008, 02:21 PM   #3
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
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
 
Old 08-22-2008, 02:55 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If you don't care what line 5 is, use this to unconditionally replace the line:
Code:
sed '5 ctrue'
ta0kira
 
Old 08-23-2008, 11:55 AM   #5
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
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
 
Old 08-23-2008, 12:04 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

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

Code:
variable=$(sed -n '3p' truefalse.txt)
echo $variable
 
Old 08-23-2008, 12:12 PM   #7
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
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
 
Old 08-23-2008, 12:13 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

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

Last edited by David the H.; 08-23-2008 at 01:38 PM.
 
Old 08-23-2008, 12:18 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by babag View Post
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.
 
Old 08-23-2008, 12:32 PM   #10
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
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

Last edited by babag; 08-23-2008 at 01:12 PM.
 
Old 08-23-2008, 01:23 PM   #11
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
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
 
Old 08-23-2008, 01:44 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


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
bash read a given line number from text bendeco13 Programming 7 08-31-2012 03:49 PM
write text to specific location in file mcbenus Linux - Desktop 3 02-28-2008 06:40 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
Writing to a specific line in a text file mrobertson Programming 6 12-30-2005 04:02 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM

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

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