LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2016, 05:00 PM   #1
soy_vivi
LQ Newbie
 
Registered: Jan 2014
Posts: 6

Rep: Reputation: Disabled
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";'
 
Old 06-21-2016, 05:17 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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

Last edited by keefaz; 06-21-2016 at 05:20 PM.
 
Old 06-21-2016, 05:26 PM   #3
soy_vivi
LQ Newbie
 
Registered: Jan 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Keefaz, thank you for your input. I'm just getting into Perl and couldn't get past this question.
 
Old 06-21-2016, 11:11 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,294
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl one-liner to remove duplicates from authorized_keys? gctaylor1 Programming 13 11-18-2015 03:24 AM
[SOLVED] perl one liner how to using sort jheengut Programming 2 11-17-2014 12:10 PM
regex or statement with perl one liner casperdaghost Linux - Newbie 4 11-13-2013 02:45 AM
Perl One-Liner to update an <a href=...> tag CDR255 Programming 4 11-02-2012 12:46 PM
perl one liner error casperdaghost Linux - Newbie 6 08-24-2011 12:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration