Why does using array and for loop breaks line into 2? [duplicate]

Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
This question already has an answer here:
How to loop over the lines of a file?
1 answer
Why does my shell script choke on whitespace or other special characters?
4 answers
Why does the following gives the line I am searching:
grep '<appointment-id internal:ref=1.2.3/>' test.xml
OUTPUT is <appointment-id internal:ref=1.2.3/>
While the following breaks it into 2 lines?
a=($(grep '<appointment-id internal:ref=1.2.3/>' test.xml))
for i in "$a[@]"; do
echo "checking $i"
grep -n "$i" delete.xml
done
The output is:
checking <appointment-id
checking internal:ref=1.2.3/>
The file is:
<note>
<to>Jim</to>
<from>John</from>
<heading>Reminder</heading>
<body>Some text</body>
<appointment-id internal:ref=1.2.3/>
</note>
bash shell-script shell
marked as duplicate by ñÃÂsýù÷, Stephen Kitt, roaima, Jesse_b, ilkkachu
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jun 7 at 20:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
-1
down vote
favorite
This question already has an answer here:
How to loop over the lines of a file?
1 answer
Why does my shell script choke on whitespace or other special characters?
4 answers
Why does the following gives the line I am searching:
grep '<appointment-id internal:ref=1.2.3/>' test.xml
OUTPUT is <appointment-id internal:ref=1.2.3/>
While the following breaks it into 2 lines?
a=($(grep '<appointment-id internal:ref=1.2.3/>' test.xml))
for i in "$a[@]"; do
echo "checking $i"
grep -n "$i" delete.xml
done
The output is:
checking <appointment-id
checking internal:ref=1.2.3/>
The file is:
<note>
<to>Jim</to>
<from>John</from>
<heading>Reminder</heading>
<body>Some text</body>
<appointment-id internal:ref=1.2.3/>
</note>
bash shell-script shell
marked as duplicate by ñÃÂsýù÷, Stephen Kitt, roaima, Jesse_b, ilkkachu
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jun 7 at 20:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
because word splitting happening per defaultIFS=$' tn'. see What is the 'IFS'?
â Ã±ÃÂsýù÷
Jun 7 at 19:03
The first solution in How to loop over the lines of a file? should work in this case too, to fill the array. The second could be used directly, without an array at all, or to build the array with array extension (a+=("$line")).
â ilkkachu
Jun 7 at 19:50
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
This question already has an answer here:
How to loop over the lines of a file?
1 answer
Why does my shell script choke on whitespace or other special characters?
4 answers
Why does the following gives the line I am searching:
grep '<appointment-id internal:ref=1.2.3/>' test.xml
OUTPUT is <appointment-id internal:ref=1.2.3/>
While the following breaks it into 2 lines?
a=($(grep '<appointment-id internal:ref=1.2.3/>' test.xml))
for i in "$a[@]"; do
echo "checking $i"
grep -n "$i" delete.xml
done
The output is:
checking <appointment-id
checking internal:ref=1.2.3/>
The file is:
<note>
<to>Jim</to>
<from>John</from>
<heading>Reminder</heading>
<body>Some text</body>
<appointment-id internal:ref=1.2.3/>
</note>
bash shell-script shell
This question already has an answer here:
How to loop over the lines of a file?
1 answer
Why does my shell script choke on whitespace or other special characters?
4 answers
Why does the following gives the line I am searching:
grep '<appointment-id internal:ref=1.2.3/>' test.xml
OUTPUT is <appointment-id internal:ref=1.2.3/>
While the following breaks it into 2 lines?
a=($(grep '<appointment-id internal:ref=1.2.3/>' test.xml))
for i in "$a[@]"; do
echo "checking $i"
grep -n "$i" delete.xml
done
The output is:
checking <appointment-id
checking internal:ref=1.2.3/>
The file is:
<note>
<to>Jim</to>
<from>John</from>
<heading>Reminder</heading>
<body>Some text</body>
<appointment-id internal:ref=1.2.3/>
</note>
This question already has an answer here:
How to loop over the lines of a file?
1 answer
Why does my shell script choke on whitespace or other special characters?
4 answers
bash shell-script shell
edited Jun 7 at 20:13
asked Jun 7 at 18:58
Jim
38710
38710
marked as duplicate by ñÃÂsýù÷, Stephen Kitt, roaima, Jesse_b, ilkkachu
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jun 7 at 20:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by ñÃÂsýù÷, Stephen Kitt, roaima, Jesse_b, ilkkachu
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jun 7 at 20:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
because word splitting happening per defaultIFS=$' tn'. see What is the 'IFS'?
â Ã±ÃÂsýù÷
Jun 7 at 19:03
The first solution in How to loop over the lines of a file? should work in this case too, to fill the array. The second could be used directly, without an array at all, or to build the array with array extension (a+=("$line")).
â ilkkachu
Jun 7 at 19:50
add a comment |Â
1
because word splitting happening per defaultIFS=$' tn'. see What is the 'IFS'?
â Ã±ÃÂsýù÷
Jun 7 at 19:03
The first solution in How to loop over the lines of a file? should work in this case too, to fill the array. The second could be used directly, without an array at all, or to build the array with array extension (a+=("$line")).
â ilkkachu
Jun 7 at 19:50
1
1
because word splitting happening per default
IFS=$' tn'. see What is the 'IFS'?â Ã±ÃÂsýù÷
Jun 7 at 19:03
because word splitting happening per default
IFS=$' tn'. see What is the 'IFS'?â Ã±ÃÂsýù÷
Jun 7 at 19:03
The first solution in How to loop over the lines of a file? should work in this case too, to fill the array. The second could be used directly, without an array at all, or to build the array with array extension (
a+=("$line")).â ilkkachu
Jun 7 at 19:50
The first solution in How to loop over the lines of a file? should work in this case too, to fill the array. The second could be used directly, without an array at all, or to build the array with array extension (
a+=("$line")).â ilkkachu
Jun 7 at 19:50
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
The output of the grep is a string containing two whitespace-separated words.
The shell will split that into two words since it's unquoted, and the array will therefore have two entries.
This will do what you want:
a=( "$( grep -F '<appointment-id internal:ref=1.2.3/>' test.xml )" )
However, parsing XML with grep is a horrible idea. Use a proper XML parser instead.
Also, the loop, if all it does is outputting a string, may be replaced by
printf 'checking %sn' "$arr[@]"
To see who made a change to a line matching a pattern in a particular revision of a git-controlled file (see comments below), use git blame -L with the pattern and revision in question. See git blame --help for further info.
Also note that to get the line number of a line matching a pattern:
sed -n '/pattern/=' file
Don't ever feed the result of grep into grep again just to get the line number. If doing so, be sure to use grep -F, or it will fail if the line contains regular expression patterns.
This conversation has been moved to chat.
â Michael Mrozekâ¦
Jun 10 at 21:12
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
The output of the grep is a string containing two whitespace-separated words.
The shell will split that into two words since it's unquoted, and the array will therefore have two entries.
This will do what you want:
a=( "$( grep -F '<appointment-id internal:ref=1.2.3/>' test.xml )" )
However, parsing XML with grep is a horrible idea. Use a proper XML parser instead.
Also, the loop, if all it does is outputting a string, may be replaced by
printf 'checking %sn' "$arr[@]"
To see who made a change to a line matching a pattern in a particular revision of a git-controlled file (see comments below), use git blame -L with the pattern and revision in question. See git blame --help for further info.
Also note that to get the line number of a line matching a pattern:
sed -n '/pattern/=' file
Don't ever feed the result of grep into grep again just to get the line number. If doing so, be sure to use grep -F, or it will fail if the line contains regular expression patterns.
This conversation has been moved to chat.
â Michael Mrozekâ¦
Jun 10 at 21:12
add a comment |Â
up vote
4
down vote
accepted
The output of the grep is a string containing two whitespace-separated words.
The shell will split that into two words since it's unquoted, and the array will therefore have two entries.
This will do what you want:
a=( "$( grep -F '<appointment-id internal:ref=1.2.3/>' test.xml )" )
However, parsing XML with grep is a horrible idea. Use a proper XML parser instead.
Also, the loop, if all it does is outputting a string, may be replaced by
printf 'checking %sn' "$arr[@]"
To see who made a change to a line matching a pattern in a particular revision of a git-controlled file (see comments below), use git blame -L with the pattern and revision in question. See git blame --help for further info.
Also note that to get the line number of a line matching a pattern:
sed -n '/pattern/=' file
Don't ever feed the result of grep into grep again just to get the line number. If doing so, be sure to use grep -F, or it will fail if the line contains regular expression patterns.
This conversation has been moved to chat.
â Michael Mrozekâ¦
Jun 10 at 21:12
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
The output of the grep is a string containing two whitespace-separated words.
The shell will split that into two words since it's unquoted, and the array will therefore have two entries.
This will do what you want:
a=( "$( grep -F '<appointment-id internal:ref=1.2.3/>' test.xml )" )
However, parsing XML with grep is a horrible idea. Use a proper XML parser instead.
Also, the loop, if all it does is outputting a string, may be replaced by
printf 'checking %sn' "$arr[@]"
To see who made a change to a line matching a pattern in a particular revision of a git-controlled file (see comments below), use git blame -L with the pattern and revision in question. See git blame --help for further info.
Also note that to get the line number of a line matching a pattern:
sed -n '/pattern/=' file
Don't ever feed the result of grep into grep again just to get the line number. If doing so, be sure to use grep -F, or it will fail if the line contains regular expression patterns.
The output of the grep is a string containing two whitespace-separated words.
The shell will split that into two words since it's unquoted, and the array will therefore have two entries.
This will do what you want:
a=( "$( grep -F '<appointment-id internal:ref=1.2.3/>' test.xml )" )
However, parsing XML with grep is a horrible idea. Use a proper XML parser instead.
Also, the loop, if all it does is outputting a string, may be replaced by
printf 'checking %sn' "$arr[@]"
To see who made a change to a line matching a pattern in a particular revision of a git-controlled file (see comments below), use git blame -L with the pattern and revision in question. See git blame --help for further info.
Also note that to get the line number of a line matching a pattern:
sed -n '/pattern/=' file
Don't ever feed the result of grep into grep again just to get the line number. If doing so, be sure to use grep -F, or it will fail if the line contains regular expression patterns.
edited Jun 7 at 21:03
answered Jun 7 at 19:05
Kusalananda
101k13199312
101k13199312
This conversation has been moved to chat.
â Michael Mrozekâ¦
Jun 10 at 21:12
add a comment |Â
This conversation has been moved to chat.
â Michael Mrozekâ¦
Jun 10 at 21:12
This conversation has been moved to chat.
â Michael Mrozekâ¦
Jun 10 at 21:12
This conversation has been moved to chat.
â Michael Mrozekâ¦
Jun 10 at 21:12
add a comment |Â
1
because word splitting happening per default
IFS=$' tn'. see What is the 'IFS'?â Ã±ÃÂsýù÷
Jun 7 at 19:03
The first solution in How to loop over the lines of a file? should work in this case too, to fill the array. The second could be used directly, without an array at all, or to build the array with array extension (
a+=("$line")).â ilkkachu
Jun 7 at 19:50