LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to read a line in a file and use it as argument (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-read-a-line-in-a-file-and-use-it-as-argument-796689/)

Alkass 03-20-2010 11:06 AM

How to read a line in a file and use it as argument
 
Hi

I want to do this

read a files's specific line but return as argument only part of it ie

...

value # this is mass

value2 # this is force

so, how can I get / use the $value and $value2 as arguments for some other file and skip the rest of the line(s) ? of course, the values are different everytime, but the comment always the same, as well as the position of the lines in the file

Thanks in advance

onebuck 03-20-2010 11:24 AM

Hi,

Welcome to LQ!

So you've shown us your need! What's your deed?
We will aid you when you help yourself to a solution. Provide us with what you have attempted and then maybe someone will be able to assist.

'Geek Stuff' has some examples.

Just a few more links to aid you to gaining some understanding;
Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Bash Reference Manual
Advanced Bash-Scripting Guide
Linux Newbie Admin Guide
LinuxSelfHelp
Getting Started with Linux

These links and others can be found at 'Slackware-Links' .
More than just SlackwareŽ links!

nonamenobody 03-20-2010 12:59 PM

Quote:

Originally Posted by Alkass (Post 3905597)
Hi

I want to do this

read a files's specific line but return as argument only part of it ie

...

value # this is mass

value2 # this is force

so, how can I get / use the $value and $value2 as arguments for some other file and skip the rest of the line(s) ? of course, the values are different everytime, but the comment always the same, as well as the position of the lines in the file

Thanks in advance

Are you doing this from the shell? is that shell bash?

Grep is the easy way to get the lines. If these are the only lines that contain the words mass and force, grep for that. If they are the only lines which contain value and value2, grep for that.
If not, you'll have to rely on the line number e.g. if it is the 3rd line use 'tail -n +3 yourfilename |head -1'.

Once you have the line you need, you can probably use cut to get what you want. E.g. cut -f 2 -d ' '

frieza 03-20-2010 02:09 PM

next step once you get a command that extracts the data from the file is to pass it to the other command
you can do that by enclosing the command in back quotes ` (shift+~) eg
Code:

echo `cat /etc/fstab` `cat /etc/fstab`
will pass the output of the cat fstab command to the echo twice, you could use that like this

Code:

command_needing_arguments `command_provide_arg1` `command_provide_arg2` `...` `command_provide_argN`
also depending on how you ultimately get the value from line(s)/word(s) for each of the arguments you could write that into a shell script that executes the big command and simply taking the line(s)/word(s) as it's arguments

hope this helps

michaelk 03-20-2010 02:19 PM

Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

Continue here:
http://www.linuxquestions.org/questi...gument-796688/


All times are GMT -5. The time now is 04:37 AM.