LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 08-06-2011, 02:49 PM   #1
DuskFall
LQ Newbie
 
Registered: Aug 2011
Posts: 18

Rep: Reputation: Disabled
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...
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 08-06-2011, 02:55 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
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

Last edited by crts; 08-06-2011 at 03:22 PM. Reason: corrected minor error
 
Old 08-06-2011, 02:56 PM   #3
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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
 
Old 08-06-2011, 03:11 PM   #4
DuskFall
LQ Newbie
 
Registered: Aug 2011
Posts: 18

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

Last edited by DuskFall; 08-06-2011 at 03:14 PM. Reason: correction
 
Old 08-06-2011, 03:20 PM   #5
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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
 
Old 08-06-2011, 03:22 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Quote:
Originally Posted by DuskFall View Post
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')
 
2 members found this post helpful.
Old 08-06-2011, 03:26 PM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Careful!

Quote:
Originally Posted by EricTRA View Post
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.
 
Old 08-06-2011, 03:29 PM   #8
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Quote:
Originally Posted by crts View Post
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
 
Old 08-06-2011, 03:35 PM   #9
DuskFall
LQ Newbie
 
Registered: Aug 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by EricTRA View Post

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...
 
Old 08-06-2011, 03:36 PM   #10
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Quote:
Originally Posted by DuskFall View Post
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
 
Old 08-06-2011, 03:42 PM   #11
DuskFall
LQ Newbie
 
Registered: Aug 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thanks again Eric and crts,

Quote:
Originally Posted by crts View Post
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...
 
Old 08-06-2011, 03:49 PM   #12
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Quote:
Originally Posted by DuskFall View Post
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
 
1 members found this post helpful.
Old 08-06-2011, 03:52 PM   #13
DuskFall
LQ Newbie
 
Registered: Aug 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Again Thank you both how do i up your rep?
 
Old 08-06-2011, 04:06 PM   #14
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Bash concatenating string to variable abercrombieande Programming 4 01-19-2011 08:04 AM
[SOLVED] BASH: if Variable -eq String not working worm5252 Programming 2 01-24-2010 04:07 PM
extract string from file to variable [BASH] NikosNL Programming 13 05-07-2008 10:43 AM
bash - pull out variables from a | deliminated string elinenbe Programming 11 02-16-2008 12:36 AM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 11:38 AM


All times are GMT -5. The time now is 08:56 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration