LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to add \n for perl one liner (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-add-%5Cn-for-perl-one-liner-4175582777/)

soy_vivi 06-21-2016 05:00 PM

How to add \n for perl one liner
 
Hello everyone,

I thank you in advance your input.

I can run this perl one line statement
Code:

[user@server ~]# perl -le 'print (1 .. 10);'
12345678910

How would I produce the same output without the -l? I have tried

I get a blank output
Code:

[user@server ~]# perl -e '$twenty = (1 .. 20); print "$twenty\n";'
I get an error message
Code:

[user@server ~]# perl -e 'print (1 .. 20); . "\n";'

keefaz 06-21-2016 05:17 PM

Code:

perl -e '$\="\n"; print (1 .. 10);'
perl -e 'use v5.10;say (1 .. 10);'
perl -e 'print 1 .. 10, "\n";'

There are surelly more ways to do it

soy_vivi 06-21-2016 05:26 PM

Keefaz, thank you for your input. I'm just getting into Perl and couldn't get past this question.

Turbocapitalist 06-21-2016 11:11 PM

While going through the examples you might check the meaning of the run time options, too. It should be in the manual page "perlrun"

Code:

man perlrun
The -n and -p are very useful as they wrap loops around your one-liner. The -i does in-place editing so this one-liner replaces the word "foo" if it is a whole word with the word "bar" through out all files with names ending in .txt The original, untouched files will end in .txt.old

Code:

perl -pi.old -e 's/\bfoo\b/bar/g' *.txt
It was a recipe something like that which piqued my interest in perl originally.


All times are GMT -5. The time now is 09:07 PM.