LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   extract string from file to variable [BASH] (https://www.linuxquestions.org/questions/programming-9/extract-string-from-file-to-variable-%5Bbash%5D-638748/)

NikosNL 04-29-2008 05:44 PM

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

jschiwal 04-29-2008 07:57 PM

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


NikosNL 04-30-2008 03:50 AM

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?

pixellany 04-30-2008 05:49 AM

Quote:

Originally Posted by NikosNL (Post 3137514)
And another question.
How can i convert a file from binary to text code?

hexedit...

NikosNL 04-30-2008 06:40 AM

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!!

NikosNL 04-30-2008 07:18 AM

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?

NikosNL 04-30-2008 08:44 AM

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


jschiwal 04-30-2008 06:49 PM

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.

NikosNL 05-01-2008 11:55 AM

hey thanks.
it works.

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

Thanks for everything!

jschiwal 05-01-2008 04:58 PM

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.

NikosNL 05-02-2008 08:15 AM

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.

jschiwal 05-07-2008 04:14 AM

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:]]/

matthewg42 05-07-2008 04:49 AM

Quote:

Originally Posted by NikosNL (Post 3137514)
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.

NikosNL 05-07-2008 09:43 AM

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!


All times are GMT -5. The time now is 06:44 AM.