Execution problem with perl

I got the below error when using the below code...it seem that perl interpret the "'" in the middle and therefore the pipe is not finished.

perl -wle '
@a=`who| perl -wlane 'print \$F[0];' | sort -u` ;
chomp @a ;
print @a;
'
the error message in cygwin is:-
perl: No match.
 | sort -u`;chomp @a ; print @a;: Command not found.

error message on Solaris is:-
Can't find string terminator "`" anywhere before EOF at -e line 2.
bash :  | sort -u
`; chomp @a ;
print @a ;
: command not found

even I try qx// instead of `` without any use.:(:(:frowning:

Thanks in advance.

BR

In your original version, the shell is closing and reopening the single-quoted string. Try:

perl -wle '
@a=`who| perl -wlane '\''print \$F[0];'\'' | sort -u` ;
chomp @a ;
print @a;
'

Whenever you need to embed single-quotes in a single-quoted string, you need to first close the single-quoted string, add a backslash-escaped single-quote, then reopen the single quoted string immediately afterwards (all of this is to appease the shell's parser, it has nothing to do with perl).

So

'

becomes

'\''

for every instance of a single quote you'd like to place inside a single-quoted string.

Regards,
Alister

Try this:

@a = qx(who| perl -wlane \47print \$F[0];\47 | sort -u);

Why don't you write it all in Perl (without embedding code)?

I tried it with the same error this was one of my tries. but with out any use.:frowning: actually the system is ignoring the 'print \$F[0]' and print all the who command o/p.

Hm ...

$ who
drado      pts/1        Mar 10 20:21    (xxx)
drado      pts/2        Mar 10 20:25    (xxx)
$ perl -wle '
  @a = qx(who| perl -wlane \47print \$F[0];\47 | sort -u);
  chomp @a;
  print @a;
'
drado

It works for me.

Sorry Alister but the problem as in radoulov solution

I would write something like this:

perl -le'
  $_{(split)[0]} = 1 for qx(who);
  print sort keys %_;
  '

really strange!!!! ...I am using SunOs 5.10, will this affect the code o/p?

Just to be sure you realize it, allow me to reiterate: The code being passed to the perl interpreter is mangled by the single-quote problem I explained in my prior post. If you are pursuing a different solution that does not use any perl code with single quotes within a single quoted string that's parsed by the shell, then cool. Nevermind this paragraph :wink:

By the way, your original code with properly-escaped single quoting (as in my first post) works fine.

Cheers,
Alister

I really don't know, it works for me on Solaris 10:

$ who
drado      pts/3        Mar 10 20:26    (xxx)
xxx   pts/2        Mar 10 20:10    (xxx)
$ uname -sr
SunOS 5.10
$ perl -wle '
  @a = qx(who| perl -wlane \47print \$F[0];\47 | sort -u);
  chomp @a;
  print @a;
'
dradoxxx

thank you guys for your reply I think some think wrong in my server I don't know where the problem is exist but I hope you could suggest to me where to start troubleshooting this.

radoulov thank you for your previous solution it worked great.

Alister:- I have also tried awk '{print $1}' instead of the inner perl in my code but with same problem!!!!

but guys is my problem related to any env variable in my system?

---------- Post updated at 21:43 ---------- Previous update was at 21:36 ----------

guys i have tried the below:-

perl -wle '
  @a = qx(who| cut -d" " -f1 | sort -u);
  chomp @a;
  print @a;
'

perl -wle '
  @a = qx(who| cut -d'\'' '\'' -f1 | sort -u);
  chomp @a;
  print @a;
'
and
perl -wle '
  @a = qx(who| cut -d\47 \47 -f1 | sort -u);
  chomp @a;
  print @a;
'

the above codes worked great with cut!!! but failed with awk and perl.

is this related to awk or perl version?

and when using below

perl -wle '
  @a = qx(who| cut -d' ' -f1 | sort -u);
  chomp @a;
  print @a;
'

the code failed.!!!

Or, of course, as alister correctly pointed out, you need to close the external/escape the embedded single quotes. alister's solution works fine for me too:

perl -wle '
@a=`who| perl -wlane '\''print \$F[0];'\'' | sort -u` ;
chomp @a ;
print @a;
'

---------- Post updated at 09:14 PM ---------- Previous update was at 08:57 PM ----------

Could you post the exact code and output you get (just copy/paste from your terminal)?

The following works fine for me.

$ perl -wle '
  @a = qx(who| cut -d'\'' '\'' -f1 | sort -u);
  chomp @a;
  print @a;
'

Perl isn't my area, but i would suggest that you take the perl code you're passing on the command line and place it in a file, then tell perl to interpret that file. If you still get an error, then it's likely that the problem is perl related. If that works fine, then you can be reasonably sure that the issue lies with your shell. Worst case scenario, install windows and code it in powershell :wink:

Best of luck,
Alister