how can i get my scripts to read/write text from/to specific parts of files?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
how can i get my scripts to read/write text from/to specific parts of files?
how can i get my scripts to read/write text from/to specific parts of files? not to be confused with by lines
like instead of cat >> to add something to the end of a file (or just > to overwrite it all), how can i tell it to, for example:
write all the text between "this bit of text" and "that bit of text" out to my seperate file, (or into another)?
so a file with:
Code:
this bit of text comes before that bit of text, and so it will only write what's between this bit of text and after that bit of text and nothing more
will write out
Code:
comes before
and after
more practically, one of the uses for this i aim to implement is to create a cheetsheet of all my keyboard shortcuts in openbox, from the rc.xml config file.
then being able to use this with imagemagick too would be great, cos then i can have constantly updated tooltip wallpaper.
i've been looking through the similar threads, and they all seem to do it only for by line/character numbers.
sed -n '/this bit of text/ {s/that bit of text/that bit of text\n/g
p}' file|sed -n '/this bit of text/ {s/^.*this bit of text \(.*\) that bit of text/\1/
p}'|grep -v "this bit of text"
I tried it with the file :
Code:
Some text
Beginnig text and this bit of text comes before that bit of text, and so it will only write what's between this bit of text and after that bit of text and nothing more this bit of text and some more text
Some other text this bit of text
Some other text
And I got the output you want :
comes before
and after
Edit : added grep -v "this bit of text" at the end, in case there is one more "this bit of text" after the last "that bit of text", or if there are lines with the text "this bit of text" but without "that bit of text" after it on the same line.
very exciting stuff. (oh jeez, it's true, i am a nerd... just not a very good one yet)
so, reading more of the man pages, replica88's thread, berbae's links and provided code, and more... and the way sed works is starting to make some small kind of sense.
tried it out on some initial dummy files, about to start adapting for practical application, and wondering how i get to that crucial part of the story, chaining it together so i can take the output (that what they call "stdout"?)from this and have it written to another file. this is a bit of bash scripting i've never grasped yet. is that what the "|" is for, chaining outputs to the next command?
(posting this garbage before i proceed to bork and learn)
fails to work for my specific needs, presuming syntactical interference from ", <, > and = in the text i'm supplanting for "this bit of text" and "that bit of text".
key, name, command, so it would be in this example, thus:
W-1 transset 0.1 transset 0.1
i tried putting a simple \ in front of the bits i thought might be messing it up causing nothing to be returned. no joy. nice stab in the dark i thought though.
/me carries on reading through the whole sed intro posted earlier.
Hope this helps.
[EDIT] You will get some output on the terminal from the echo commands but it won't affect the newTest.xml file's content. Maybe there is a better way to avoid or at least minimize use of echo.
To crts, you can prevent the undesired outputs coming from grep (not echo) with the -q option :
grep -q "<keybind"
grep -q "<name>"
grep -q "<command>"
I tested your script on a real openbox rc.xml with many stuff in it, and many "keybind key=" sections, but only one with a <name> and <command> in it (it is the rc.xml I use).
Code:
time ./crts_script
real 0m5.706s
user 0m4.073s
sys 0m2.257s
cat newTest.xml
W-e Konqueror kfmclient openProfile filemanagement
With my script I get :
Code:
time ./script >result
real 0m0.016s
user 0m0.007s
sys 0m0.010s
cat result
"W-e" Konqueror kfmclient openProfile filemanagement
As you can see, your script is simpler in its writing, but also much slower.
To crts, you can prevent the undesired outputs coming from grep (not echo) with the -q option :
grep -q "<keybind"
grep -q "<name>"
grep -q "<command>"
Hi,
thanks for hint with grep -q. About the time I get a different result.
(It runs a bit faster with the '-q' option than without)
Code:
$ time ./crts.filter
real 0m0.794s
user 0m0.344s
sys 0m0.324s
$ time ./berbae.filter >> berbae.result
real 0m0.083s
user 0m0.036s
sys 0m0.004s
It is still slower but not by that much as on your machine. Also yours runs slower on my machine although I used only the xml-file with 4 keybinds
Anyway, I wanted to avoid the 'while' loop and also do it with sed first. But I must admit I have only started recently working with 'sed' and my knowledge of its full potential is not that deep yet.
However, although my script is not time-optimized I think it is easier to modify so that it will also work with slightly modified input-files.
BTW: Is the '.*' after your '<keybind' in the first line really necessary? I removed it and the script still worked. It also ran slightly faster.
OK the time differs with the xml file treated and the machine on which the script turns.
Nevertheless, you can see that sed is very quick to do complex tasks.
But also not so clear in its syntax and to maintain a script.
So... It's a choice of what matters the most.
But Siljrath seems to have disappeared now, so I will leave that thread now.
Have good success, crts, in your script and program writings.Bye!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.