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

  • Home
  • SEARCH
  • 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 236611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:21:32+00:00 2026-05-11T20:21:32+00:00

What language should I use for file and string manipulation? This might seem objective,

  • 0

What language should I use for file and string manipulation?

This might seem objective, but really isn’t I think. There’s lot to say about this. For example I can see clearly that for most usages Perl would be a more obvious candidate than Java. I need to do this quite often and at this time I use C# for it, but I would like a more scriptlike language to do this.

I can imagine Perl would be a candidate for it, but I would like to do it in PowerShell since PowerShell can access the .NET library (easy). Or is Python a better candidate for it? If I have to learn a new language, Python is certainly one on my list, rather than Perl.

What I want to do for example, is to read a file, make some changes and save it again. E.g.: open it, number all lines (say with 3 digits) and close it.
Any example, in any language, would be welcome, but the shorter the better. It is utility scripting I’m after here, not OO, TDDeveloped, unit-tested stuff of course.

What I would very much like to see is something as (pseudocode here):

open foobar.as f

foreach  line in f.lines 
 line.addBefore(currenIteratorCounter.format('ddd') + '. ')

close f

So:

bar.txt 

Frank Zappa
Cowboy Henk
Tom Waits

numberLines bar.txt

bar.txt 

001. Frank Zappa
002. Cowboy Henk
003. Tom Waits

UPDATE:

The Perl and Python examples here are great, and definitely in the line of what I was hoping and expecting. But aren’t there any PowerShell guys out there?

  • 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-11T20:21:32+00:00Added an answer on May 11, 2026 at 8:21 pm

    This is actually pretty easy in PowerShell:

    function Number-Lines($name) {
        Get-Content $name | ForEach-Object { $i = 1 } { "{0:000}. {1}" -f $i++,$_ }
    }
    

    What I’m doing here is getting the contents of the file, this will return a String[], over which I iterate with ForEach-Object and apply a format string using the -f operator. The result just drops out of the pipeline as another String[] which can be redirected to a file if needed.

    You can shorten it a little by using aliases:

    gc .\someFile.txt | %{$i=1}{ "{0:000}. {1}" -f $i++,$_ }
    

    but I won’t recommend that for a function definition.

    You way want to consider using two passes, though and constructing the format string on the fly to accommodate for larger numbers of lines. If there are 1500 lines {0:000} it won’t be sufficient anymore to get neatly aligned output.

    As for which language is best for such tasks, you might look at factors such as

    • conciseness of code (Perl will be hard to beat there, especially that one-liner in another answer)
    • readability and maintainability of code
    • availability of the tools (Perl and Python aren’t installed on Windows by default (PowerShell only since Windows 7), so deployment might be hindered.)

    In the light of the last point you might even be better off using cmd for this task. The code is similarly pretty simple:

    @echo off
    setlocal
    set line=1
    for /f "delims=" %%l in (%1) do call :process %%l
    endlocal
    goto :eof
    
    :process
    call :lz %line%
    echo %lz%. %*
    set /a line+=1
    goto :eof
    
    :lz
    if %1 LSS 10 set lz=00%1&goto :eof
    if %1 LSS 100 set lz=0%1&goto :eof
    set lz=%1&goto :eof
    goto :eof
    

    That assumes, of course, that it has to run somewhere else than your own machine. If not, then use whatever fits your needs 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 117k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The error indicates that the image you're trying to change… May 11, 2026 at 10:41 pm
  • Editorial Team
    Editorial Team added an answer I think I just figured it out myself. In the… May 11, 2026 at 10:41 pm
  • Editorial Team
    Editorial Team added an answer Just sanitize your DB entries and you'll be ok. Rails… May 11, 2026 at 10:41 pm

Related Questions

I am really new to Haskell (Actually I saw Real World Haskell from O'Reilly
How can I compile java code from an arbitrary string (in memory) in Java
For simplicity, i have the following file named test.jsp: <script language=javascript> alert(a$b.replace(/\$/g,k)); </script> I
I've got several large MFC applications here, and converting them into any other format

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.