LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-09-2006, 03:35 PM   #1
flipwhy
Member
 
Registered: Jul 2006
Posts: 102

Rep: Reputation: 15
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?

i hope its clear as crystal...:-)
 
Old 11-09-2006, 03:56 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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..
 
Old 11-09-2006, 04:04 PM   #3
hockeyman_102
Member
 
Registered: Apr 2006
Location: Washington
Distribution: Suse, CentOS, Ubuntu
Posts: 124

Rep: Reputation: 15
why can't you write a c program, or perl, or whatever you like? Does it have to be cli?
 
Old 11-09-2006, 04:13 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
why would you bother to write in c for this? it looks like it will be really trivial to achieve with a script.
 
Old 11-09-2006, 04:45 PM   #5
flipwhy
Member
 
Registered: Jul 2006
Posts: 102

Original Poster
Rep: Reputation: 15
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
 
Old 11-09-2006, 05:24 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
Code:
$ cat one.int ; cat two.int
2.213 4 437.5 45.1111 3.14139 23 7 888 43.4 4.11 322 5.1
5.1 322 4.11 43.4 888 7 23 3.14139 45.1111 437.5 4 2.213
$ first_number=$(cut -d" " -f 8 one.int)
$ second_number=$(cut -d" " -f 4 two.int)
$ echo "$first_number + $second_number" | bc > result.int
$ cat result.int
931.4
 
Old 11-09-2006, 05:29 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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:
Code:
$ first_number=$(awk '{ print $8 }' one.int)
$ second_number=$(awk '{ print $4 }' two.int)
...
 
Old 11-09-2006, 05:44 PM   #8
flipwhy
Member
 
Registered: Jul 2006
Posts: 102

Original Poster
Rep: Reputation: 15
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)?
 
Old 11-09-2006, 06:13 PM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by flipwhy
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:
Code:
$ first_number=$(cut -d" " -f 8 one.int)
$ second_number=$(cut -d" " -f 4 two.int)
$ result=$(echo "$first_number + $second_number" | bc)
$ sed -i "3s/.*/$result/" result.int
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.
 
Old 11-10-2006, 04:07 AM   #10
flipwhy
Member
 
Registered: Jul 2006
Posts: 102

Original Poster
Rep: Reputation: 15
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...
 
Old 11-10-2006, 04:18 AM   #11
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
If all you want to do is append the result to the file, there's no need to use sed, you can just use the append re-direct:
Code:
$ echo "$result" >> result.int
 
Old 11-10-2006, 11:16 AM   #12
flipwhy
Member
 
Registered: Jul 2006
Posts: 102

Original Poster
Rep: Reputation: 15
sorry... one more thing... i would actually like the number from line 2 to be taken...
and also is there a way to copy it and not cut it out?
 
Old 11-10-2006, 11:26 AM   #13
flipwhy
Member
 
Registered: Jul 2006
Posts: 102

Original Poster
Rep: Reputation: 15
sorry... i realised it doesnt cut..but copies i thought it did for some reason...
but im still trying to find how t ospecify the second line..
 
Old 11-10-2006, 12:00 PM   #14
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
If you want to print a number from a particular line and you're already using awk, you might do it like this:
Code:
first_number=$(awk '{ if ( NR==2 ) { print $8 } }' one.int)
 
Old 11-10-2006, 07:11 PM   #15
flipwhy
Member
 
Registered: Jul 2006
Posts: 102

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


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
print a text file with long lines and line number added powah Linux - General 2 05-26-2006 02:02 PM
Read in an Octal number from a text file using C++ pjordan Programming 2 11-18-2004 03:03 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM
"how do I extract a number from a text file using shell command?" sdandeker Linux - Networking 3 02-12-2004 08:54 AM
bash script to download a number of things linksocc Linux - Software 3 12-10-2003 12:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:58 AM.

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