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 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 |
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=", "} |
Thanks made :cool:
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? |
Quote:
|
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!! |
Code:
bt ~ # eval $(awk 'BEGIN {FS=", "}/temp/{ print "var1="$1,"var2="$4 }' qsutf8.txt) What do I do wrong? |
Code:
bt ~ # var1=$(awk 'BEGIN {FS=", "}/user/{ print $1 }' qsutf8.txt) It works But now I want every part in a new array compartment How can I do that?? Code:
bt ~ # echo array[0] |
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. |
hey thanks.
it works. but why use eval in front? whitout eval it works fine! Thanks for everything! |
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. |
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, 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:..:..:..:..:../ |
There are quite a few regular expressions that will match.
Code:
/sbin/ifconfig | sed -rn '/([[:xdigit:]][[:xdigit:]]:){5}/p' 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:]]/ |
Quote:
Code:
od -tc file.bin > file.txt |
thanks matthew!
jschiwal: can i use: Code:
/([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]].*([[:xdigit:]][[:xdigit:]]:){5}[[:xdigit:]][[:xdigit:]]/ |
All times are GMT -5. The time now is 06:44 AM. |