Assignment of field using echo...

Assignment of field using echo...

Post by ragh » Thu, 03 Jul 2003 19:21:23



Hi All,

First posting on this forum.

I am experiencing a scenario which I try to describe here.
Input is a multiline file (actually an xml file), out of which space
delimited data fields are extracted using

Line 1: for i in `cat file_name`
Line 2: do
Line 3: var="`echo $i`"
Line 4: echo $i >> temp_out
Line 5: done

if the data field contains something like "2245123<stringchars", line
3 comes up to

var="`echo 2245123<stringchars`"

which fails... due to possible redirection confusions (that's what I
think).

My question is, if this is the problem, this should happen always, I
mean with all kinds of input data files having such data field
combinations. But its not. Its occuring very inconsistently.

Some input files with similar situations are failing at this line and
some are not.

Any guesses... Pls...

Expecting some guidence...

regards
raghav..

__________________
raghav

 
 
 

Assignment of field using echo...

Post by Barry Margoli » Thu, 03 Jul 2003 23:42:03




>Hi All,

>First posting on this forum.

>I am experiencing a scenario which I try to describe here.
>Input is a multiline file (actually an xml file), out of which space
>delimited data fields are extracted using

>Line 1: for i in `cat file_name`
>Line 2: do
>Line 3: var="`echo $i`"

Why do you need echo there?  Why not just:

var="$i"

In fact, what's $var for anyway?  You never use it!

Quote:>Line 4: echo $i >> temp_out
>Line 5: done

>if the data field contains something like "2245123<stringchars", line
>3 comes up to

>var="`echo 2245123<stringchars`"

>which fails... due to possible redirection confusions (that's what I
>think).

It shouldn't.  The only processing one on the result of variable expansion
is word splitting and wildcard expansion.  Redirection characters should
not be processed specially.

Quote:>My question is, if this is the problem, this should happen always, I
>mean with all kinds of input data files having such data field
>combinations. But its not. Its occuring very inconsistently.

Since I don't think it should happen at all, I'm not sure.

But in any case, the way to avoid all special processing of the variable
expansion is to put it in quotes:

echo "$i" >> temp_out

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Assignment of field using echo...

Post by ragh » Fri, 04 Jul 2003 02:24:19





> >Hi All,

> >First posting on this forum.

> >I am experiencing a scenario which I try to describe here.
> >Input is a multiline file (actually an xml file), out of which space
> >delimited data fields are extracted using

> >Line 1: for i in `cat file_name`
> >Line 2: do
> >Line 3: var="`echo $i`"

> Why do you need echo there?  Why not just:

> var="$i"

Its put there to remove possible initial blanks in the field. e.g. if
the field contains "      <tag>Data Value</tag>"
I only want the non blank part (i mean trim the initial spaces).

if i do it like you said, echo $i >> temp_out, the spaces will go with
it. or wonn't they ? Correct me if I am wrong.

Quote:

> In fact, what's $var for anyway?  You never use it!

Sorry , but here it was my typo. its actually -
Line 4: echo $var >> temp_out

- Show quoted text -

Quote:> >Line 4: echo $i >> temp_out
> >Line 5: done

> >if the data field contains something like "2245123<stringchars", line
> >3 comes up to

> >var="`echo 2245123<stringchars`"

> >which fails... due to possible redirection confusions (that's what I
> >think).

> It shouldn't.  The only processing one on the result of variable expansion
> is word splitting and wildcard expansion.  Redirection characters should
> not be processed specially.

> >My question is, if this is the problem, this should happen always, I
> >mean with all kinds of input data files having such data field
> >combinations. But its not. Its occuring very inconsistently.

> Since I don't think it should happen at all, I'm not sure.

> But in any case, the way to avoid all special processing of the variable
> expansion is to put it in quotes:

> echo "$i" >> temp_out

Yeah, You r right its a solution, but I still don't understand why is
this happening. That too inconsistently. Out of about 100 program
executions instances, it failed just once.

thanks and regards
raghav..

 
 
 

Assignment of field using echo...

Post by Chris F.A. Johnso » Fri, 04 Jul 2003 02:47:50



> Hi All,

> First posting on this forum.

> I am experiencing a scenario which I try to describe here.
> Input is a multiline file (actually an xml file), out of which space
> delimited data fields are extracted using

> Line 1: for i in `cat file_name`
> Line 2: do
> Line 3: var="`echo $i`"

     The command substitution and echo do nothing. The line can be
     eliminated. Note that $i will not contain any leading or trailing
     spaces.

Quote:> Line 4: echo $i >> temp_out
> Line 5: done

     You could do the same thing with:

tr -s ' \t' '\n' file_name > tmp_out

     Some versions of tr will require '\011' instead of '\t' and
     '\012' instead of '\n'.

Quote:> if the data field contains something like "2245123<stringchars", line
> 3 comes up to

> var="`echo 2245123<stringchars`"

> which fails... due to possible redirection confusions (that's what I
> think).

     What error messages do you get?

Quote:> My question is, if this is the problem, this should happen always, I
> mean with all kinds of input data files having such data field
> combinations. But its not. Its occuring very inconsistently.
> Some input files with similar situations are failing at this line and
> some are not.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
 
 
 

Assignment of field using echo...

Post by Barry Margoli » Fri, 04 Jul 2003 02:54:31








>> >Hi All,

>> >First posting on this forum.

>> >I am experiencing a scenario which I try to describe here.
>> >Input is a multiline file (actually an xml file), out of which space
>> >delimited data fields are extracted using

>> >Line 1: for i in `cat file_name`
>> >Line 2: do
>> >Line 3: var="`echo $i`"

>> Why do you need echo there?  Why not just:

>> var="$i"
>Its put there to remove possible initial blanks in the field. e.g. if
>the field contains "      <tag>Data Value</tag>"
>I only want the non blank part (i mean trim the initial spaces).

It will also trim internal spaces.  For instance, if the field contains
"<tag>Data Value</tag>" it will become "<tag>Data Value</tag>".

Quote:>if i do it like you said, echo $i >> temp_out, the spaces will go with
>it. or wonn't they ? Correct me if I am wrong.

No, they won't.  For the same reason that the blanks are removed in the
assignment.  It's not the assignment that's removing the blanks, it's the
use of echo with an unquoted argument.

Quote:>> But in any case, the way to avoid all special processing of the variable
>> expansion is to put it in quotes:

>> echo "$i" >> temp_out

>Yeah, You r right its a solution, but I still don't understand why is
>this happening. That too inconsistently. Out of about 100 program
>executions instances, it failed just once.

Me either.  What specific error do you get?

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Assignment of field using echo...

Post by Bill Marcu » Thu, 03 Jul 2003 23:51:02


On 2 Jul 2003 03:21:23 -0700, raghav

> Hi All,

> First posting on this forum.

> I am experiencing a scenario which I try to describe here.
> Input is a multiline file (actually an xml file), out of which space
> delimited data fields are extracted using

> Line 1: for i in `cat file_name`
> Line 2: do
> Line 3: var="`echo $i`"

Why not var=$i  or  var="$i" ?

--
It is often the case that the man who can't tell a lie thinks he is the best
judge of one.
                -- Mark Twain, "Pudd'nhead Wilson's Calendar"

 
 
 

1. Using awk or cut to print every field after the 11th field

Hi folks,
can anyone think of a neat way to print field 11 onwards
from various line with the following shape.
N.B. The number of fields can vary, but I always want to
print from field 11 until the last field inclusive.

user1  29299  0.0  0.2 1024  776 ?    S 08:10:00  0:00 sh -c /home/user/testscr

Thanks in advance
Paul

2. System Accounting

3. assignment in echo?

4. using several uplinks on masqed network? loadbalancing?

5. Using Random Port Assignment With ipchains/masq

6. Filesystem difference between 1.2.13 and 2.0.30??

7. variable assignment uses up input to script (123)

8. Bus error of Netscape under Linux

9. Suggest an assignment using iptables and SNAT/DNAT

10. echo $$ ; (echo $$)

11. ICMP echo and port 7 echo service

12. How do I extract a field between two other fields?

13. Local Echo vs Host Echo for tty's