1

So I've done a research about the perl -pe command and I know that it takes records from a file and creates an output out of it in a form of another file. Now I'm a bit confused as to how this line of command works since it's a little modified so I can't really figure out what exactly is the role of perl pe in it. Here's the command:

cd /usr/kplushome/entities/Standalone/config/webaccess/WebaccessServer/etc
(PATH=/usr/ucb:$PATH; ./checkall.sh;) | perl -pe "s,^,          ,g;"

Any idea how it works here?

What's even more confusing in the above statement is this part : "s,^, ,g;"

Any help would be much appreciated. Let me know if you guys need more info. Thank you!

melpomene
  • 84,125
  • 8
  • 85
  • 148
Francis
  • 94
  • 10

2 Answers2

3

It simply takes an expression given by the -e flag (in this case, s,^, ,g) and performs it on every line of the input, printing the modified line (i.e. the result of the expression) to the output.

The expression itself is something called a regular expression (or "regexp" or "regex") and is a field of learning in and of itself. Quick googles for "regular expression tutorial" and "getting started with regular expressions" turn up tons of results, so that might be a good place to start.

This expression, s,^, ,g, adds ten spaces to the start of the line, and as I said earlier, perl -p applies it to every line.

melpomene
  • 84,125
  • 8
  • 85
  • 148
mwp
  • 8,217
  • 20
  • 26
  • The `-e` option takes a whole program (sequence of statements), not just an expression. `s///` is not a regex, it's a substitution operator (the first part of it is a regex, though: `s/REGEX/REPLACEMENT/`). – melpomene Nov 05 '15 at 06:59
  • Yes, I could have explained a lot of other things that would go over a new programmer's head. – mwp Nov 05 '15 at 07:14
  • You could have explained things that aren't wrong. Also, `/g` doesn't make sense with `^`, and the whole thing can be replaced by `-e '$_ = " $_"'` (there are supposed to be more spaces here but apparently comments filter them out) or `-e '$_ = " " x 10 . $_'`, bypassing the whole regex mess. (Your code actually adds 11 spaces despite the text claiming otherwise.) – melpomene Nov 05 '15 at 07:18
  • It's not **my** code, it's the OP's code, I am simply trying to explain it in a way that he or she will understand. Lose the chip and post your own answer if you're not happy with the ones that are here. I'll be happy to nitpick it to death and commit horrendous logical fallacies when you do. – mwp Nov 05 '15 at 07:25
  • OP's code adds 10 spaces. The one in your answer adds 11. – melpomene Nov 05 '15 at 07:26
  • I must have copied and pasted wrong (or the question was edited after I pasted and before I counted). I'll fix it. Great catch, sherlock. – mwp Nov 05 '15 at 07:31
1
"s,^,          ,g;"

s is use for substitution. syntax is s/somestring/replacement/.
In your command , is the delimiter instead of /.
g is for work globally, means replace all occurrence.

For example:

perl -p -i -e "s/oldstring/newstring/g" file.txt;

In file.txt all oldstring will replace with newstring.
i is for inplace file editing.

See these doc for information:

perlre
perlretut
perlop

serenesat
  • 4,611
  • 10
  • 37
  • 53
  • The code is not replacing `^`; `^` is a regex metacharacter meaning "beginning of string" (and it doesn't make sense to use `g` with a regex anchored like that). – melpomene Nov 05 '15 at 07:20