need a number from a text file and do things with it - how?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
need a number from a text file and do things with it - how?
is it possible to get a script to pick out a certain string or number from a horizontal list of them and add it to another in a nother file and put the answer in another file...
for example..
if i cat something and it gives me for example:
0.765 678 495.2331
and i want it to take 678 and add it to another number in a similar file and paste the answer into another file in a specific line..
how would you do that?
well yes that's totally possible with bog standard unix tools, e.g. awk, grep etc... but you'd need to provide a *lot* more info about the files you are using... what formats do they actually take? what commonality is there to allow a reliable parsing for the right information etc..
umm...
ok its a .int file and it has a list of numbers of varying lengths separated with spaces and i want to take the 8th number and add it to another number in a .intv file... its just like a text file. and the answer would have to go in a specific location in a .txt file.
How about a lil example of a bash script..coz i duno how to select a certain character
How many lines in each file? If there is only one it's pretty straight forward, else you have to provide us with some information about how you choose which line you wish to take the number from.
Assuming there's only one line per file, and your two source files are one.int and two.int, and you want to take the 8th field from one.int and add it to the fourth field of two.int, here's one method: I'm assuming there's only one space between the fields, and that they may be decimal numbers (i.e. not just integers). The output goes in result.int.
If you have possibly more than one space and/or a mixture of spaces and tabs between the fields, cut using a de-limiter might not get the field you want. In this case cut will see "1 space 2 space space 3" as "1, 2, [empty field], 3".
In this case awk is nice because by default splits fields by any combination of whitespace, so the commands would become:
alright...thats cool.. thanks...
is there also a way to specify where in the result.int file it pastes the answer (like can you specify a line and column maybe)?
alright...thats cool.. thanks...
is there also a way to specify where in the result.int file it pastes the answer (like can you specify a line and column maybe)?
Oh I see, result.int already exists? The commands I provided would replace any existing result.int with a file containing just the result number. If the file doesn't exist before the command, it will be created.
If you want to make a substitution within an existing file, you'll probably want to use sed or something similar.
Replacing a whole line with the result is quite easy. Here we replace the whole of line 3 with the result:
Note that this will give an error if result.int doesn't exist, or do nothing if there is no line 3 to replace.
If you have some known string you with to replace, for example you have some template output file with a unique string in it __RESULT__, which you will replace with the actual result, that's easy too:
Code:
$ result="3.14159265359"
$ cat result.int
A little song...
I've got __RESULT__ legs from my hips to the ground,
And when I move em they walk around....
$ sed -i "s/__RESULT__/$result/" result.int
$ cat result.int
A little song...
I've got 3.14159265359 legs from my hips to the ground,
And when I move em they walk around....
I'm not sure how to specify a field number using sed.
ok... thanks a lot..
one more question... i looked in the man sed pages and tried some of the append options but theyre not doing what i want which is to append the answers to the results.int file. any idea?
in particular i tried the -a but it didnt work...
alright...that works well..
but now it keeps giving a parse error... and im pretty sure its because the number is in the form 2e-03 i.e. 0.002...
anyway of counteracting that...please?
i hope this wil be the last of the problems...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.