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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
08-22-2008, 02:35 PM
|
#1
|
Member
Registered: Aug 2003
Posts: 426
Rep:
|
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 02:37 PM.
|
|
|
08-22-2008, 02:49 PM
|
#2
|
Member
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896
Rep:
|
Quote:
Originally Posted by babag
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.
|
|
|
08-22-2008, 03:21 PM
|
#3
|
Member
Registered: Aug 2003
Posts: 426
Original Poster
Rep:
|
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
|
|
|
08-22-2008, 03:55 PM
|
#4
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep:
|
If you don't care what line 5 is, use this to unconditionally replace the line: ta0kira
|
|
|
08-23-2008, 12:55 PM
|
#5
|
Member
Registered: Aug 2003
Posts: 426
Original Poster
Rep:
|
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
|
|
|
08-23-2008, 01:04 PM
|
#6
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
To get the output of a command, put it inside "$()".
Code:
variable=$(sed -n '3p' truefalse.txt)
echo $variable
|
|
|
08-23-2008, 01:12 PM
|
#7
|
Member
Registered: Aug 2003
Posts: 426
Original Poster
Rep:
|
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
|
|
|
08-23-2008, 01:13 PM
|
#8
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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 02:38 PM.
|
|
|
08-23-2008, 01:18 PM
|
#9
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Quote:
Originally Posted by babag
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.
|
|
|
08-23-2008, 01:32 PM
|
#10
|
Member
Registered: Aug 2003
Posts: 426
Original Poster
Rep:
|
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 02:12 PM.
|
|
|
08-23-2008, 02:23 PM
|
#11
|
Member
Registered: Aug 2003
Posts: 426
Original Poster
Rep:
|
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
|
|
|
08-23-2008, 02:44 PM
|
#12
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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 08:03 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|