LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Pull string from a file into a variable using bash (https://www.linuxquestions.org/questions/linux-newbie-8/pull-string-from-a-file-into-a-variable-using-bash-895945/)

DuskFall 08-06-2011 01:49 PM

Pull string from a file into a variable using bash
 
The subject explains it really... i have a file (.tmpfile) and inside it is a string which i only know part of, the rest being a random group of characters... I would like to know how to pull the whole string out of the file and into a variable.
Thanks in advance...

crts 08-06-2011 01:55 PM

Hi and welcome to LQ,

Try this:
var=$(sed -rn 's/.*([^[:blank:]]*variable[^[:blank:]]*).*/\1/p' file)

If that does not do the trick then you will have to post the file and what exactly you want to extract

EricTRA 08-06-2011 01:56 PM

Hello and Welcome to LinuxQuestions,

You could use grep for that. Since grep is greedy it will save the whole line where the regex is encountered. It would help though if you could include an example of your 'string'. For example if I have a file which has among other lines the following text:
Code:

This is a simplistic regular regex grep
taking into account that the word simplistic for example only occurs on that one line, you could use this:
Code:

VARIABLE=$(grep simplistic <yourfilename.tmpfile>)
Have a look at the man page for grep for more details.

Looking forward to your participation in the forums. Have fun with Linux.

Kind regards,

Eric

DuskFall 08-06-2011 02:11 PM

Thanks for the welcome guys :)
Crts, thank you for that i'll try that in a second... and Eric, it wasn't quite like that... should have been more specific, i need to pull 100000597065197 from :

Code:

<dict>
    <key>100000597065197</key>
</dict>

except, all the characters after "1000" are random so i wouldnt know them... i would think it would be something with awk or grep but i' unfamiliar with them both...
I know the key tag is malformed, ive already pulled this section of code from a different file...

EricTRA 08-06-2011 02:20 PM

Hi,

No problem at all. That only happens if you ask a questions with a broad possibility of answers. The more specific your questions, the more defined the answer. From your example you could use, in line with what ctrs posted:
Code:

VARIABLE=$(sed -i ‘s#.*<key>\(.*\)<\/key>.*#\1#g' yourfile)
Which would grab everything between the 'key' tags.

Hope it helps.

Kind regards,

Eric

crts 08-06-2011 02:22 PM

Quote:

Originally Posted by DuskFall (Post 4435447)
Thanks for the welcome guys :)
Crts, thank you for that i'll try that in a second... and Eric, it wasn't quite like that... should have been more specific, i need to pull 100000597065197 from :

Code:

<dict>
    <key>100000597065197</key>
</dict>

except, all the characters after "1000" are random so i wouldnt know them... i would think it would be something with awk or grep but i' unfamiliar with them both...
I know the key tag is malformed, ive already pulled this section of code from a different file...

In this case you cannot use the above 'sed'. Try this instead:
Code:

var=$(sed -rn 's|.*<key>(.*)</key>|\1|p')
# or
var=$(sed -rn 's|.*(1000[[:digit:]]*).*|\1|p')
# or
var=$(sed -rn 's|.*<key>(1000[[:digit:]]*)</key>|\1|p')


crts 08-06-2011 02:26 PM

Careful!
 
Quote:

Originally Posted by EricTRA (Post 4435453)
Code:

VARIABLE=$(sed -i ‘s#.*<key>\(.*\)<\/key>.*#\1#g' yourfile)

Be very careful with this! The '-i' option will modify your file. From what I have understood so far this is not what you want.
@Eric: The '-i' option will prevent any output, so VARIABLE would be empty afterwards.

EricTRA 08-06-2011 02:29 PM

Quote:

Originally Posted by crts (Post 4435458)
Be very careful with this! The '-i' option will modify your file. From what I have understood so far this is not what you want.
@Eric: The '-i' option will prevent any output, so VARIABLE would be empty afterwards.

Hi crts,

You're absolutely right! I'm sorry. I just copied part of a full line without even thinking. This is what it should have been:
Code:

VARIABLE=$(cat yourfile | grep "<key" | sed 's#.*<key>\(.*\)<\/key>.*#\1#g')
Again, sorry for the confusion.

EDIT: your solution looks so much neater! Yet learned other ways. Thanks.

Kind regards,

Eric

DuskFall 08-06-2011 02:35 PM

Quote:

Originally Posted by EricTRA (Post 4435453)

Code:

VARIABLE=$(sed -i ‘s#.*<key>\(.*\)<\/key>.*#\1#g' yourfile)

Tried this, i got EOF errors... sorry
slightly confused, which one am i doing? Sorry again people...

EricTRA 08-06-2011 02:36 PM

Quote:

Originally Posted by DuskFall (Post 4435466)
Tried this, i got EOF errors... sorry
slightly confused, which one am i doing? Sorry again people...

Hi,

As pointed out by crts, don't use that one. I goofed up :( The second one should work though, but the solutions offered by crts look a lot better and less typing.

Kind regards,

Eric

DuskFall 08-06-2011 02:42 PM

Thanks again Eric and crts,

Quote:

Originally Posted by crts (Post 4435455)
Code:

var=$(sed -rn 's|.*<key>(.*)</key>|\1|p')

Where do i mention the file in your code? Because i'm either blind or not looking hard enough...

EricTRA 08-06-2011 02:49 PM

Quote:

Originally Posted by DuskFall (Post 4435472)
Thanks again Eric and crts,


Where do i mention the file in your code? Because i'm either blind or not looking hard enough...

Hi,

At the end of the line just before the closing ).

Kind regards,

Eric

DuskFall 08-06-2011 02:52 PM

Again Thank you both :) how do i up your rep? :)

EricTRA 08-06-2011 03:06 PM

Hi,

Already 'upped' apparently. Thanks. Glad you've got a solution. And thanks for marking your thread solved. Have fun with Linux.

Kind regards,

Eric


All times are GMT -5. The time now is 04:09 PM.