Operating System - HP-UX
1828242 Members
2524 Online
109975 Solutions
New Discussion

Re: How do I store command output/extract file contents?

 
SOLVED
Go to solution
Mr. Lewis Lampkin, III
Frequent Advisor

How do I store command output/extract file contents?

Basically, I want to run a command, then store the output of the command, so that it can be used later on in a scrypt.

How can I do this?


Hrm, I was just thinking about it, maybe this would work:

command > filename

Then, later on, I want to extract the contents of the nth line within that 'filename', and use it somewhere else.

how could I do that?

Thanks.
7 REPLIES 7
R.K. #
Honored Contributor

Re: How do I store command output/extract file contents?

Hi..

>>>command > filename
yes..this will work.

To get 9th line, you can use sed:
#cat | sed -n 9p

Regds..
Don't fix what ain't broke
Must123
Occasional Advisor

Re: How do I store command output/extract file contents?

you can use the command, It will give you the nth line you want.

command |tee filename | head - |tail -1
Mr. Lewis Lampkin, III
Frequent Advisor

Re: How do I store command output/extract file contents?

Hrm. .... how do I use that later on in a script?

I need to store the value in that line somehow, so that it can be inserted later.

I guess it helps if I go into a bit more detail.

i have a command, encrypt6.1, which generates encrypted passwords

it looks like this

hostname:/dir1/dir2 >encrypt6.1 password
ldfadslfie.1#!l1
hostname:/dir1/dir2 >

I was thinking I could do this:
hostname:/dir1/dir2 >encrypt6.1 password > pwordfile

I then later on, want to use what is inside that file, and I don't know how to do that.

basically, if I could figure out how to get the contents of the pword file, it would be called %encrypted%.

What I then want to do is this

hostname:/dir1/dir2/dir3 >string1:string2:%encrypted%:string3 >> pwordfile


So, if you can follow my pseudocode, I want to do this:

(1) Generate an encrypted password, and store it as the variable %encrypted% (not sure of how to do this)

(2) Later on, use this variable, by appending to a file.

I'm not sure how to do the variable part, but maybe now that I have explained a bit more, you can explain to me better how to achieve the result that I want?
Mr. Lewis Lampkin, III
Frequent Advisor

Re: How do I store command output/extract file contents?

OK, sorry, I just used some bad logic in my explanation.

There should not be two "pwordfile" in the explanation above.

Replace one of the two with someone else.

I am unable to edit the post.

Thanks.
James R. Ferguson
Acclaimed Contributor
Solution

Re: How do I store command output/extract file contents?

Hi:

> So, if you can follow my pseudocode, I want to do this:
(1) Generate an encrypted password, and store it as the variable %encrypted% (not sure of how to do this)
(2) Later on, use this variable, by appending to a file.

Shell variables are composed of letters, digits, or underscores. A letter or underscore must be the starting character. Hence, you could store the result of a command like this:

# DATE_VAL=$(date)

...and append this variable's value to a file thusly:

# echo ${DATE_VAL} >> myfile

Regards!

...JRF...
Mr. Lewis Lampkin, III
Frequent Advisor

Re: How do I store command output/extract file contents?

James:

Thanks. I'm glad that you were able to figure out what I was asking for, better than I could explain it.

So, in my case, I could do this:

# encrypt_string=$(encrypt6.1 password)
# echo string1:string2:${encrypt_string}:string3 >> usersfile

Thanks, you fully understood what I needed.
Mr. Lewis Lampkin, III
Frequent Advisor

Re: How do I store command output/extract file contents?

Closed the thread, James showed me how to store the command output as a variable, which I could then use later on in a script.

Others provided help with extracting data from the nth line of a file, which can come in handy later.

Thanks all!