Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

So if I saved system_profile to a text document, how would I be able to delete all but one section? Or delete quotes around it? Then how would I assign certain text to a variable to check certain things?

Example: If I save system_profile to a text document, how would I search through to find how much ram I have, and assign that to the variable 'ram'? Also, could I have it search for and delete certain characters? For example have a script delete all quotation marks?

share|improve this question
    
Can you edit your question to provide examples of what you want? Your question is too vague to answer. –  Chris Ting May 22 '11 at 3:42
    
I agree with Chris. Can you provide a sample of what system_profile will look like? The sample needs to be as accurate as possible to ensure that the solution will work correctly. –  Bandit May 22 '11 at 4:00
    
This question is really three separate questions. You've asked about performing a text search, editing a file, and variable assignment. Please ask your questions separately. –  Chris Ting May 22 '11 at 4:52

2 Answers 2

up vote 0 down vote accepted

You can search for words in a file using grep.

Example:

cat system_profile | grep ram

This will return the line in system_profile that contains the pattern "ram". You'll probably need to run the command and look at the return before you can develop an understanding of what to do next. If all you want is a portion of the file, you can perform a search and redirect your output to a new file.

Example:

cat system_profile | grep ram -A 5 > system_ram

This will give you the line that contains "ram" and the five lines after it.

You can replace characters thru the utility sed.

Example:

sed -i 's/old/new/g' system_profile

To eliminate double quotes ("):

sed -i 's/"//g' system_profile
share|improve this answer
    
Is it possible to also include the next line or few lines? Or prior lines? And if it appears several times how do i specify which occourance? Then what about saving it to a variable? –  JShoe May 22 '11 at 4:04
    
As Bandit has shown, it's possible to include lines before and after your search returns positive. If you post the lines in your file just before and after the value you want to save, we can help you. –  Chris Ting May 22 '11 at 4:35

I'm not entirely sure how to select between different occurrences, but hopefully this will help.

x=$(cat system_profile | grep ram -A 5)

The above code will save the line containing ram and the next 5 lines after it. For getting lines before, use -a instead of -A.

I guess grep supports selecting a limited number of lines (i.e. stop after the first occurrence of ram). If that is what you are looking for, use the -m 1 (where one is the number of occurrences to find before stopping) option for grep.

Alternatively, you can modify your grep search if the two (or more) lines getting selected are not identical. Just change ram to whatever you need to match (put it in quotes if it is more than one word)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.