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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-29-2008, 05:44 PM
|
#1
|
|
LQ Newbie
Registered: Apr 2008
Posts: 9
Rep:
|
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
|
|
|
|
04-29-2008, 07:57 PM
|
#2
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
04-30-2008, 03:50 AM
|
#3
|
|
LQ Newbie
Registered: Apr 2008
Posts: 9
Original Poster
Rep:
|
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.
|
|
|
|
04-30-2008, 05:49 AM
|
#4
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Quote:
Originally Posted by NikosNL
And another question.
How can i convert a file from binary to text code?
|
hexedit...
|
|
|
|
04-30-2008, 06:40 AM
|
#5
|
|
LQ Newbie
Registered: Apr 2008
Posts: 9
Original Poster
Rep:
|
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!!
|
|
|
|
04-30-2008, 07:18 AM
|
#6
|
|
LQ Newbie
Registered: Apr 2008
Posts: 9
Original Poster
Rep:
|
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?
|
|
|
|
04-30-2008, 08:44 AM
|
#7
|
|
LQ Newbie
Registered: Apr 2008
Posts: 9
Original Poster
Rep:
|
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.
|
|
|
|
04-30-2008, 06:49 PM
|
#8
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
05-01-2008, 11:55 AM
|
#9
|
|
LQ Newbie
Registered: Apr 2008
Posts: 9
Original Poster
Rep:
|
hey thanks.
it works.
but why use eval in front?
whitout eval it works fine!
Thanks for everything!
|
|
|
|
05-01-2008, 04:58 PM
|
#10
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
05-02-2008, 08:15 AM
|
#11
|
|
LQ Newbie
Registered: Apr 2008
Posts: 9
Original Poster
Rep:
|
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.
|
|
|
|
05-07-2008, 04:14 AM
|
#12
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
05-07-2008, 04:49 AM
|
#13
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Quote:
Originally Posted by NikosNL
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.
|
|
|
|
05-07-2008, 09:43 AM
|
#14
|
|
LQ Newbie
Registered: Apr 2008
Posts: 9
Original Poster
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:09 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|