Modify string on command line

M

Markus Dehmann

What's the easiest way to modify a string on the command line (using
perl)?

I want to change
a/b/c/d
to
a_b_c_d

The following line does it, but it's long and cumbersome:
perl -e '$ARGV[0] =~ s#/#_#g; print $ARGV[0]' "a/b/c/d"

Isn't there a special perl switch that puts $ARGV[0] into $_ or so?
Then, I could just say:
perl -e 's#/#_#g; print' "a/b/c/d"

And it would be better without the "print" ...

Thanks!
Markus
 
A

Anno Siegel

Markus Dehmann said:
What's the easiest way to modify a string on the command line (using
perl)?

I want to change
a/b/c/d
to
a_b_c_d

The following line does it, but it's long and cumbersome:
perl -e '$ARGV[0] =~ s#/#_#g; print $ARGV[0]' "a/b/c/d"

Isn't there a special perl switch that puts $ARGV[0] into $_ or so?
Then, I could just say:
perl -e 's#/#_#g; print' "a/b/c/d"

No switch, but you can use "for":

perl -le 'tr{/}{-} and print for shift' a/b/c/d
And it would be better without the "print" ...

Huh?

Anno
 
B

Brian Wakem

Markus said:
What's the easiest way to modify a string on the command line (using
perl)?

I want to change
a/b/c/d
to
a_b_c_d

The following line does it, but it's long and cumbersome:
perl -e '$ARGV[0] =~ s#/#_#g; print $ARGV[0]' "a/b/c/d"

Isn't there a special perl switch that puts $ARGV[0] into $_ or so?
Then, I could just say:
perl -e 's#/#_#g; print' "a/b/c/d"

And it would be better without the "print" ...


$ echo "a/b/c/d" | perl -pe 's#/#_#g'
a_b_c_d
 
T

Tad McClellan

Markus Dehmann said:
What's the easiest way to modify a string on the command line (using
perl)?

I want to change
a/b/c/d
to
a_b_c_d


perl -le 'tr#/#_#, print for @ARGV' "a/b/c/d"

Isn't there a special perl switch that puts $ARGV[0] into $_ or so?


perl -e '$_=shift; tr#/#_#; print' "a/b/c/d"

And it would be better without the "print" ...


echo "a/b/c/d" | perl -pe 'tr#/#_#'
 
A

axel

Markus Dehmann said:
What's the easiest way to modify a string on the command line (using
perl)?
I want to change
a/b/c/d
to
a_b_c_d
The following line does it, but it's long and cumbersome:
perl -e '$ARGV[0] =~ s#/#_#g; print $ARGV[0]' "a/b/c/d"
Isn't there a special perl switch that puts $ARGV[0] into $_ or so?
Then, I could just say:
perl -e 's#/#_#g; print' "a/b/c/d"

One method would be:

echo "a/b/c/d" | perl -pe 's#/#_#g'

Axel
 
M

Markus Dehmann

Markus said:
What's the easiest way to modify a string on the command line (using
perl)?

I want to change
a/b/c/d
to
a_b_c_d

The following line does it, but it's long and cumbersome:
perl -e '$ARGV[0] =~ s#/#_#g; print $ARGV[0]' "a/b/c/d"

Isn't there a special perl switch that puts $ARGV[0] into $_ or so?
Then, I could just say:
perl -e 's#/#_#g; print' "a/b/c/d"

And it would be better without the "print" ...

Thanks for your answers! Based on your suggestions, the following is
also possible, as I just found out:

perl -pe 's#/#_#g' <(echo "a/b/c")

(not that it's better, but it's an interesting variation using process
substitution ...)

Markus
 
L

Lazrado Allan \(IFIN CSW WS MS\)

Markus Dehmann said:
Markus said:
What's the easiest way to modify a string on the command line (using
perl)?

I want to change
a/b/c/d
to
a_b_c_d

The following line does it, but it's long and cumbersome:
perl -e '$ARGV[0] =~ s#/#_#g; print $ARGV[0]' "a/b/c/d"

Isn't there a special perl switch that puts $ARGV[0] into $_ or so?
Then, I could just say:
perl -e 's#/#_#g; print' "a/b/c/d"

And it would be better without the "print" ...

Thanks for your answers! Based on your suggestions, the following is
also possible, as I just found out:

perl -pe 's#/#_#g' <(echo "a/b/c")

(not that it's better, but it's an interesting variation using process
substitution ...)

Markus

The following also works fine.(a variant of what you described)
echo "a/b/c" | perl -pe 's#/#_#g'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top