LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-29-2008, 05:44 PM   #1
NikosNL
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Rep: Reputation: 0
extract string from file to variable [BASH]


Hello

Im making a program, and I need to extract string from a file to variable.

FILE EXAMPLE:
Code:
xdsaxdas, dxasxdsa, dasdxas, dsageg, da3a3
xdsf3das, dx628dsa, d147xas, ds25eg, da343
I use the command egrep '*dasdxas*' FILE
to display the line with dasdxas.

now i need the first part before the comma. And put that in a variable
and I need the third one. and put that one in another variable.

and this with all line that include dasdxas

Does someone know how to do that?

and another problem is that the file is binary and i have to manual save it as text. is there a bash command or script to convert binary file to text file?

Thanks,

and sorry for the bad languages!

Thanks!

Nikos
 
Old 04-29-2008, 07:57 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You should indicate which value you want into variables. The third entry is dasxdas itself. Regular expressions are very exacting, so your description needs to be exact. You are clear on the input but not on the results you want. It seems from your description that you want the 1st and 3rd field but you already know the 3rd, so that can't be what you meant.

Your regex input to egrep is wrong. You are using filesystem type wildcards. The asterisk character needs to be preceded by a character or regular expression:
egrep '.*dasdxas.*' FILE

or simply egrep 'dasdxas' FILE

You can extract a part of the input using awk or grep:
var1=$(sed -n '/dasdxas/s/^\([[:alnum:]][[:alnum:]]*\),.*/\1/p;/' FILE)

Because of the structure of the input, awk would work better:
Code:
eval $(awk 'BEGIN {FS=", "}
/dasdxas/{ print "var1="$1,"var2="$4 }' FILE)
~> echo $var1
xdsaxdas
~> echo $var2
dsageg

Last edited by jschiwal; 04-29-2008 at 07:59 PM.
 
Old 04-30-2008, 03:50 AM   #3
NikosNL
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks made

And is this working for all the lines?

Im going to look at it now.

THansk made!!!

And another question.
How can i convert a file from binary to text code?

Last edited by NikosNL; 04-30-2008 at 05:44 AM.
 
Old 04-30-2008, 05:49 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by NikosNL View Post
And another question.
How can i convert a file from binary to text code?
hexedit...
 
Old 04-30-2008, 06:40 AM   #5
NikosNL
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Original Poster
Rep: Reputation: 0
I find out how to convert binary to utf8 thanks.

Im now making my program.
And i will report the progress.

Thanks for helping jschiwal!!
 
Old 04-30-2008, 07:18 AM   #6
NikosNL
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Code:
bt ~ # eval $(awk 'BEGIN {FS=", "}/temp/{ print "var1="$1,"var2="$4 }' qsutf8.txt)
-bash: 5: command not found
Its not working.

What do I do wrong?
 
Old 04-30-2008, 08:44 AM   #7
NikosNL
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Code:
bt ~ # var1=$(awk 'BEGIN {FS=", "}/user/{ print $1 }' qsutf8.txt)
bt ~ # echo $var1
usertest1 usertest2 usertest3
Thanks

It works

But now I want every part in a new array compartment
How can I do that??

Code:
bt ~ # echo array[0]
usertest1
bt ~ # echo array[1]
usertest2
bt ~ # echo array[2]
usertest3

Last edited by NikosNL; 04-30-2008 at 08:54 AM.
 
Old 04-30-2008, 06:49 PM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You can fill an array with the notation: name=(value value value ...)
So have your awk command list just the values. Perhaps use printf instead of print so that
the results are output on the same line for all the records.
eval name=( $(awk ' ... { printf "%s %s ", $1,$4"}') )

For a file with 10 records, this will create an array variable filled with the values from
the first and forth field of all the records. Because I didn't use "\n" in the format for
printf, they all appear on the same line of the output.

For a very large file, this could cause a problem with too many arguments, or not enough
memory for the array variable to contain all the elements.

Last edited by jschiwal; 04-30-2008 at 09:58 PM.
 
Old 05-01-2008, 11:55 AM   #9
NikosNL
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Original Poster
Rep: Reputation: 0
hey thanks.
it works.

but why use eval in front?
whitout eval it works fine!

Thanks for everything!
 
Old 05-01-2008, 04:58 PM   #10
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Eval is usually used when you dynamically construct a command and its arguments.
In the last example, I was editing my first example and didn't need it because only the argument was added. In my previous example, the awk command returned the LFS (left-hand-side) as well. When you do that, you need to use eval.

Here is a common usage of eval, if you use ssh and don't want to retype the passphrase every time in a session:
eval $(ssh-agent)
ssh-add

The ssh-agent command returns with three or four assignment statements, so the eval is needed.

Last edited by jschiwal; 05-02-2008 at 06:20 AM.
 
Old 05-02-2008, 08:15 AM   #11
NikosNL
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Original Poster
Rep: Reputation: 0
aha OK thanks!

I have another question.
It's about regular expression.

I have a file with MAC addresses:

Code:
00:XX:XX:XX:XX:XX, 2008-04-30 21:54:34, 2008-04-30 22:25:43, [...]  0,  16, NAME,

...

Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs
00:XX:XX:XX:XX:XX, 2008-04-30 22:02:15, 2008-04-30 22:21:13,   98,       26, 00:XX:XX:XX:XX:XX, NAME

I only want to use the lines with 2 MAC's
what for expression do I need to use then?


I tried this:
Code:
/00:..:..:..:..:..*00:..:..:..:..:../
But it is not working.
 
Old 05-07-2008, 04:14 AM   #12
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
There are quite a few regular expressions that will match.

Code:
/sbin/ifconfig | sed -rn '/([[:xdigit:]][[:xdigit:]]:){5}/p'
eth0      Link encap:Ethernet  HWaddr 00:0F:B0:0C:EF:AB
eth1      Link encap:Ethernet  HWaddr 00:90:4B:92:71:A1
eth2      Link encap:Ethernet  HWaddr 00:04:5A:9D:D6:E8
jschiwal@hpamd64:~/work/apue/chapter_4> /sbin/ifconfig | sed -rn '/([[:xdigit:]][[:xdigit:]]:){5}/p'
I used the -r for extended regular expressions.
If I wanted to be more exact, I would have used
/([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]]/
The a sequence like 00:1b:20:45:56 wouldn't produce a false positive. If you are producing a generalized sed script to use in a program, you might want to be more exact than if you are working interactively in the shell.

---

Oops, you want two of them.
/([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]].*([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]]/

Last edited by jschiwal; 05-07-2008 at 04:16 AM.
 
Old 05-07-2008, 04:49 AM   #13
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by NikosNL View Post
How can i convert a file from binary to text code?
Code:
od -tc file.bin > file.txt
od is "octal dump". Useful for finding embedded NULLs and other stuff in your files. See the manual page for options which produce alternative output formats.
 
Old 05-07-2008, 09:43 AM   #14
NikosNL
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Original Poster
Rep: Reputation: 0
thanks matthew!

jschiwal:

can i use:
Code:
/([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]].*([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]]/
in awk? or do i have to at parameters?
Thanks!

Last edited by NikosNL; 05-08-2008 at 10:04 AM.
 
  


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
bash search for a pattern within a string variable nutthick Programming 7 03-06-2012 03:54 AM
how to extract paragraphs from file in BASH script followed by prefix ! , !! and !!! nabmufti Programming 4 02-10-2008 09:23 AM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 10:38 AM
Variable in Bash get from other file?? helptonewbie Programming 4 08-21-2006 07:51 AM
BASH: Replace string in file with another eur0dad Programming 5 07-27-2006 04:29 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration