Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6637973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:22:45+00:00 2026-05-25T23:22:45+00:00

I have a perl one liner that supposed to export each individual line in

  • 0

I have a perl one liner that supposed to export each individual line in an xml file to it’s own separate file with the name of the original file and then the line number within that file that it came from.

For instance, if the xml file is called “foo.xml” and it has 100 lines in it then I want to have a hundred files called, “foo_1.xml”, “foo_2.xml”, “foo_3.xml”, etc.

I thought that the name of the file that I pass to the one liner would be available via ARGV, but it’s not. I’m getting a “uninitialized value in $ARGV[0]” error when I run this:

perl -nwe 'open (my $FH, ">", "$ARGV[0]_$.\.xml"); print $FH $_;close $FH;' foo.xml

What am I overlooking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:22:46+00:00Added an answer on May 25, 2026 at 11:22 pm

    When using the magic <> filehandle (which you’re doing implicitly with the -n option), Perl shifts the filenames out of @ARGV as it opens them. (This is mentioned in perlop.) You need to use the plain scalar $ARGV, which contains the filename currently being read from:

    perl -nwe 'open (my $FH, ">", "${ARGV}_$.\.xml"); print $FH $_;close $FH;' foo.xml
    

    (The braces are necessary because $ARGV_ is a legal name for a variable.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl one-liner that works fine on the command line: perl -nle
Suppose you have a file that contains IP addresses, one address in each line:
I have a Perl script that launches 2 threads,one for each processor. I need
I have a perl one-liner in a Makefile to edit files in place: perl
Is there an Awk or Perl one-liner that can remove all rows that do
Supposed I have a file with Perl-code: does somebody know, if there is a
I am attempting to write a one-line Perl script that will toggle a line
In Perl, I'm trying to read a file line by line and process each
I have a large word list file with one word per line. I would
Suppose I have a command cmd1 that reads one line of input from standard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.