Extract information into large variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract information into large variable
# 1  
Old 10-27-2011
Extract information into large variable

Hello people Smilie

That's here my first message to your forum, so I guess it would be fine ^^

I have a request about a code I want to use.

Actually, my system use a large variable, including much informations but those informations can change by more and I want to extract one of thoses informations.
For exemple I want to extract the information between
Code:
<add key="LevelName" value="

and
Code:
" type="System.String,mscorlib" />

and use it as a new variable

levelname=$(codeforextractinformationhere)

Here is an exemple of my large variable =>

Code:
<?xml version="1.0" encoding="utf-16" standalone="yes"?> <values> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="Log" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="Admin" value="true" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="LevelName" value="blabla" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="Fastdisk1" value="" type="System.String,mscorlib" /> <add key="Fastdisk2" value="" type="System.String,mscorlib" /> <add key="Fastdisk3" value="" type="System.String,mscorlib" /> <add key="backupinterval" value="300" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="" type="System.String,mscorlib" /> <add key="" value="true" type="System.String,mscorlib" /> <add key="backupeverything" value="false" type="System.String,mscorlib" /></values>

So if anyone have a solution for me ? Smilie

Thankssss
# 2  
Old 10-27-2011
Code:
$ perl -e '
@xml=<>;
chomp @xml;
$xml=join "",@xml;
@levels=$xml=~m/<add\skey="LevelName"\svalue="#so everything after this should be captured
(.+?)# but in a non-greedy match
"\stype="System.String,mscorlib"\s\/># and only if it is followed by this text
/gx;#the x modifier allows comments in the regex and the g modifier matches all instances
print "We found the following levels in the file:\n";
for $level(@levels){
   print "\t$level\n";
}' ~/tmp.dat
We found the following levels in the file:
        blabla


Last edited by Skrynesaver; 10-27-2011 at 05:56 AM.. Reason: Added comments to explain regex's action
# 3  
Old 10-27-2011
Quote:
Originally Posted by Skrynesaver
Code:
 perl -e ' 
@xml=<>;
chomp @xml;
$xml=join "",@xml;
@levels=$xml=~m/<add key="LevelName" value="(.+?)" type="System.String,mscorlib" \/>/;
print "We found the following levels in the file:\n";
for $level(@levels){
   print "\t$level\n";
}' ~/tmp.dat
We found the following levels in the file:
        blabla

That's look like nice, but I wish to use it into a bash shell script

"#!/bin/bash", this will be a variable used by another command.

How I can include your code ?
# 4  
Old 10-27-2011
This should work, there should however be a paragraph sized comment beside it explaining what it does for future maintainers or else write the earlier script to a file including comments and call that script from your shell script.
Code:
#!/bin/bash
levelName=$(perl -e '@x=<>;$x=join "",@x;($l)=$x=~m/<add key="LevelName" value="(.+?)" type="System.String,mscorlib" \/>/;print "$l\n";' ~/tmp.dat)
myNextCommand $levelName

# 5  
Old 10-27-2011
Code:
$ echo `cat infile` | tr '=' '\n' | nawk -F'"' '/type/ {print $2}'| grep '.'
true
blabla
300
true
false

# 6  
Old 10-27-2011
Ok perfect ^^

I will try to use it, but one last thing, can you tell me how I can use it by calling another variable or into a txt file ?

I mean, this variable into wich I cut this information is for exemple ${bigvariable}, and so how I include that to extract my new variable LevelName from it ?
# 7  
Old 10-27-2011
redirect the above ouput to a flat file and assign it to a variable ..
Code:
$ echo `cat infile` | tr '=' '\n' | nawk -F'"' '/type/ {print $2}'| grep '.' > tempfile
$ LevelName=`cat tempfile` 
$ echo $LevelName
true blabla 300 true false

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to extract 8 characters from a large file.

Hi All!! I have a large file containing millions of records. My purpose is to extract 8 characters immediately from the given file. 222222222|ZRF|2008.pdf|2008|01/29/2009|001|B|C|C 222222222|ZRF|2009.pdf|2009|01/29/2010|001|B|C|C 222222222|ZRF|2010.pdf|2010|01/29/2011|001|B|C|C... (5 Replies)
Discussion started by: pavand
5 Replies

2. Shell Programming and Scripting

Extract information from file

In a particular directory, there can be 1000 files like below. filename is job901.ksh #!/bin/ksh cront -x << EOJ submit file=$PRODPATH/scripts/genReport.sh maxdelay=30 &node=xnode01 tname=job901 &pfile1=/prod/mldata/data/test1.dat ... (17 Replies)
Discussion started by: vedanta
17 Replies

3. Shell Programming and Scripting

Extract information from file

Gents, If is possible please help. I have a big file (example attached) which contends exactly same value in column, but from column 2 to 6 these values are diff. I will like to compile for all records all columns like the example attached in .csv format (output.rar ).. The last column in the... (11 Replies)
Discussion started by: jiam912
11 Replies

4. UNIX for Dummies Questions & Answers

Extract spread columns from large file

Dear all, I want to extract around 300 columns from a very large file with almost 2million columns. There are no headers, but I can find out which column numbers I want. I know I can extract with the function 'cut -f2' for example just the second column but how do I do this for such a large... (1 Reply)
Discussion started by: fndijk
1 Replies

5. Shell Programming and Scripting

How to extract specific information?

hi, i have a file A like this: ******************* No 2823 ******************** contig15205- G383C4U02H4G80+ is in contig15205- G383C4U02HGLXM- is in contig15205- G383C4U01C3HIZ+ is in contig15205- ... (3 Replies)
Discussion started by: the_simpsons
3 Replies

6. Shell Programming and Scripting

How to extract information from a file?

Hi, i have a file like this: <Iteration> <Iteration_iter-num>3</Iteration_iter-num> <Iteration_query-ID>lcl|3_0</Iteration_query-ID> <Iteration_query-def>G383C4U01EQA0A length=197</Iteration_query-def> <Iteration_query-len>197</Iteration_query-len> ... (9 Replies)
Discussion started by: the_simpsons
9 Replies

7. Shell Programming and Scripting

Extract data from large file 80+ million records

Hello, I have got one file with more than 120+ million records(35 GB in size). I have to extract some relevant data from file based on some parameter and generate other output file. What will be the besat and fastest way to extract the ne file. sample file format :--... (2 Replies)
Discussion started by: learner16s
2 Replies

8. Shell Programming and Scripting

Extract large list of substrings

I have a very long string (millions of characters). I have a file with start location and length that is thousands of rows long: Start Length 5 10 16 21 44 100 215 37 ... I'd like to extract the substring that corresponds to the start and length from each row of the list: I tried... (7 Replies)
Discussion started by: dcfargo
7 Replies

9. Shell Programming and Scripting

AWK to extract information

Hi all, I am working on a shell script to extract information from a file that has output from Oracle sqlplus. The problem is that the output of a single line is spread across multiple lines and i do not know as how to extract the particular filed at ones,which spans multiple lines.... (2 Replies)
Discussion started by: harris2107
2 Replies

10. Shell Programming and Scripting

How do you extract the information from a library?

hi all, i am quite new with Perl (just 1 week of experience) and i am suppose to understand the usage of library, which i can't, anyway... i came across this library...i think from the linux redhat redhat.linux.lib.pl, there are a couple of functions there that can be used to retrieve information... (3 Replies)
Discussion started by: mercz
3 Replies
Login or Register to Ask a Question