About other solutions with code more than 3 lines.
I can't believe we have to reinvent the wheel for such a simple task by writing 25 lines of code.. I will be ashamed of using Shells..
Your question was phrased a little awkwardly. You got what you asked for, but perhaps not what you wanted -- I don't see a reason why you actually need to use a pipe there at all. The pipe is what's making it so complicated.
Given that, did you try my solution?
Code:
var="string with spaces"
# This kind of string splitting is controlled by IFS too!
for X in $var # note that $var must NOT be quoted here!
do
echo "$X"
done
Last edited by Corona688; 12-15-2011 at 02:23 PM..
Hi,
I have a variable which has a list of string separated by comma.
for ex ,
Variable=/usr/bin,/usr/smrshbin,/tmp
How can i get the values between the commas separately using shell scripts.Please help me.
Thanks,
Padmini. (6 Replies)
Taking inputs in the script which is space separated and passing this to a function and i have assigned like below
And then when I use for loop for the inputs i got from the user, it is taking only the first argument.
Enter Names : Bala Sundar Sridhar
read names
namesCheck $names
function... (7 Replies)
Hi, i have this text:
X (m) 4917536.9627 4917536.9673 0.0090 -0.0046
Y (m) -815726.1383 -815726.1294 0.0061 -0.0089
Z (m) 3965857.4730 3965857.4840 0.0071 -0.0110
X (m) 4917536.9627 4917537.1411 -0.1784 0.1710
Y (m) -815726.1383 -815726.4859 0.3476 0.3489
Z (m) 3965857.4730... (2 Replies)
Hi
pls help me to get the code:
i have a file in which content is :
2.01304E+11 2.01304E+11 ori 2 01:00
2.01304E+11 2.01304E+11 ori 2 01:02
2.01304E+11 2.01304E+11 ori 3 01:02
2.01304E+11 2.01304E+11 ori 3 ... (7 Replies)
Hi,
I have a large number of files which are written as csv (comma-separated values).
Does anyone know of simple sed/awk command do achieve this?
Thanks!
---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ----------
Guess I asked this too soon. Found the... (0 Replies)
hello experts,
i am retrieving values in variables jobKey and jobName within my shell script. these values are returned to me within braces and i am using following command to remove those braces:
jobKeys=`echo $jobKeys | sed 's:^.\(.*\).$:\1:'`
jobNames=`echo $jobNames | sed... (1 Reply)
Hi
I have been racking my (limited) brains to get this to work without success
I have a file output which is a list of lists - ie a single column of data that is separated by space into sub lists below - I need to both split this so that each list is in a separate column (eg tab or semicolon... (8 Replies)
I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables.
I need to read this file which is an input to my script
Config.txt
file name, first path, second... (7 Replies)
Hi,
I am having a file say list1 with a output like below
jun 12 18:23
may 20 18:23
Now i want to pass the above two values into for loop,I have written a script like this.
#!/bin/bash
a=`cat list1`
for i in $a
do
echo "HI $i"
done
expected output:
HI jun 12 18:23 (3 Replies)
Hello,
I have a file which has the following contents :
thewall............0000000000200000 kmemfreelater......0000000000000000
kmemgcintvl........0000000000000002 kmeminuse..........00000000223411C0
allocated..........0000000029394000 bucket.......... @.F1000A02800C2158
The mentioned... (4 Replies)
Discussion started by: rahul2662
4 Replies
LEARN ABOUT PHP
xml_parse_into_struct
XML_PARSE_INTO_STRUCT(3) 1 XML_PARSE_INTO_STRUCT(3)xml_parse_into_struct - Parse XML data into an array structureSYNOPSIS
int xml_parse_into_struct (resource $parser, string $data, array &$values, [array &$index])
DESCRIPTION
This function parses an XML string into 2 parallel array structures, one ($index) containing pointers to the location of the appropriate
values in the $values array. These last two parameters must be passed by reference.
PARAMETERS
o $parser
- A reference to the XML parser.
o $data
- A string containing the XML data.
o $values
- An array containing the values of the XML data
o $index
- An array containing pointers to the location of the appropriate values in the $values.
RETURN VALUES xml_parse_into_struct(3) returns 0 for failure and 1 for success. This is not the same as FALSE and TRUE, be careful with operators such as
===.
EXAMPLES
Below is an example that illustrates the internal structure of the arrays being generated by the function. We use a simple note tag embed-
ded inside a para tag, and then we parse this and print out the structures generated:
Example #1
xml_parse_into_struct(3) example
<?php
$simple = "<para><note>simple note</note></para>";
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array
";
print_r($index);
echo "
Vals array
";
print_r($vals);
?>
When we run that code, the output will be:
Index array
Array
(
[PARA] => Array
(
[0] => 0
[1] => 2
)
[NOTE] => Array
(
[0] => 1
)
)
Vals array
Array
(
[0] => Array
(
[tag] => PARA
[type] => open
[level] => 1
)
[1] => Array
(
[tag] => NOTE
[type] => complete
[level] => 2
[value] => simple note
)
[2] => Array
(
[tag] => PARA
[type] => close
[level] => 1
)
)
Event-driven parsing (based on the expat library) can get complicated when you have an XML document that is complex. This function does
not produce a DOM style object, but it generates structures amenable of being transversed in a tree fashion. Thus, we can create objects
representing the data in the XML file easily. Let's consider the following XML file representing a small database of aminoacids informa-
tion:
Example #2
moldb.xml - small database of molecular information
<?xml version="1.0"?>
<moldb>
<molecule>
<name>Alanine</name>
<symbol>ala</symbol>
<code>A</code>
<type>hydrophobic</type>
</molecule>
<molecule>
<name>Lysine</name>
<symbol>lys</symbol>
<code>K</code>
<type>charged</type>
</molecule>
</moldb>
And some code to parse the document and generate the appropriate objects:
Example #3
parsemoldb.php - parses moldb.xml into an array of molecular objects
<?php
class AminoAcid {
var $name; // aa name
var $symbol; // three letter symbol
var $code; // one letter code
var $type; // hydrophobic, charged or neutral
function AminoAcid ($aa)
{
foreach ($aa as $k=>$v)
$this->$k = $aa[$k];
}
}
function readDatabase($filename)
{
// read the XML database of aminoacids
$data = implode("", file($filename));
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $values, $tags);
xml_parser_free($parser);
// loop through the structures
foreach ($tags as $key=>$val) {
if ($key == "molecule") {
$molranges = $val;
// each contiguous pair of array entries are the
// lower and upper range for each molecule definition
for ($i=0; $i < count($molranges); $i+=2) {
$offset = $molranges[$i] + 1;
$len = $molranges[$i + 1] - $offset;
$tdb[] = parseMol(array_slice($values, $offset, $len));
}
} else {
continue;
}
}
return $tdb;
}
function parseMol($mvalues)
{
for ($i=0; $i < count($mvalues); $i++) {
$mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
}
return new AminoAcid($mol);
}
$db = readDatabase("moldb.xml");
echo "** Database of AminoAcid objects:
";
print_r($db);
?>
After executing parsemoldb.php, the variable $db contains an array of AminoAcid objects, and the output of the script confirms that:
** Database of AminoAcid objects:
Array
(
[0] => aminoacid Object
(
[name] => Alanine
[symbol] => ala
[code] => A
[type] => hydrophobic
)
[1] => aminoacid Object
(
[name] => Lysine
[symbol] => lys
[code] => K
[type] => charged
)
)
PHP Documentation Group XML_PARSE_INTO_STRUCT(3)