Extract information into large variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract information into large variable
# 15  
Old 10-27-2011
It worked when I tested it (on bash, iirc)! Is that the same input?

Also, what version of sed do you have?

EDIT:
For tip78's code, you should just be able to replace the sed command with the perl command:
Code:
levelname=$(echo "${bigvariable}" | perl -ne 'while(/value="(.*?)"/gi){print"$1\n" if(length$1>1)}')

I think, anyway - I don't use perl very often. Or use a tempfile as you did earlier.

Last edited by CarloM; 10-27-2011 at 06:03 PM..
This User Gave Thanks to CarloM For This Post:
# 16  
Old 10-27-2011
Quote:
Originally Posted by CarloM
It worked when I tested it (on bash, iirc)! Is that the same input?

Also, what version of sed do you have?

EDIT:
For tip78's code, you should just be able to replace the sed command with the perl command:
Code:
levelname=$(echo "${bigvariable}" | perl -ne 'while(/value="(.*?)"/gi){print"$1\n" if(length$1>1)}')

I think, anyway - I don't use perl very often. Or use a tempfile as you did earlier.
Sed version 4.2.1

I tested this code =>

Code:
bigvariable="<?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>"

levelname=$(echo "${bigvariable}" | sed 's:.*<add key="LevelName" value="\([^"]*\)" type="System.String,mscorlib" />.*:\1:g')
echo ${levelname} >> /home/+LOGS+/bla-test
cat /home/+LOGS+/bla-test

That's result by the "bigvariable" without quotes =>

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>

# 17  
Old 10-27-2011
Use single quotes on the bigvariable assignment:
Code:
bigvariable='<?xml version="1.0" etc etc >'

With double quotes you're effectively passing a list of strings which will be concatenated - without the double quotes - so bigvariable will be:
Code:
<?xml version=1.0 encoding=utf-16 standalone=yes?> <values> <add key= value= type=System.String,mscorlib /> etc etc


Last edited by CarloM; 10-27-2011 at 06:51 PM..
# 18  
Old 10-28-2011
Code:
levelname=$(echo "${bigvariable}" | sed 's:.*<add key="LevelName" value="\([^"]*\)" type="System.String,mscorlib" />.*:\1:g')

This one work perfectly CarloM Smilie
Thanks Smilie

Last step I guess, when I use it into my full script system that's don't work, so I will try to extract the information from a flat file.
bigvariable will be writed in a single file.
How I can adapt your command with a flat file ?

I tried with this one without sucess =>
Code:
levelname=$(echo sed 's:.*<add key="LevelName" value="\([^"]*\)" type="System.String,mscorlib" />.*:\1:g' home/+LOGS+/99)

on this one too Smilie
Code:
levelname=$(echo "home/+LOGS+/99" | sed 's:.*<add key="LevelName" value="\([^"]*\)" type="System.String,mscorlib" />.*:\1:g')

# 19  
Old 10-28-2011
Code:
levelname=$(sed 's:.*<add key="LevelName" value="\([^"]*\)" type="System.String,mscorlib" />.*:\1:g' bigvariablefile)

This User Gave Thanks to CarloM For This Post:
# 20  
Old 10-28-2011
Quote:
Originally Posted by CarloM
Code:
levelname=$(sed 's:.*<add key="LevelName" value="\([^"]*\)" type="System.String,mscorlib" />.*:\1:g' bigvariablefile)

I'm sorry but that's don't work :x

Code:
levelname=$(sed 's:.*<add key="LevelName" value="\([^"]*\)" type="System.String,mscorlib" />.*:\1:g' /home/+LOGS+/99)
echo $levelname

Print all the bigvariable informations
# 21  
Old 10-28-2011
What is the output of cat /home/+LOGS+/99? (Have you lost the quotes again?)
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