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 7925337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:15:16+00:00 2026-06-03T18:15:16+00:00

I have a Linux folder tree with mixed shell, Perl and Python scripts. None

  • 0

I have a Linux folder tree with mixed shell, Perl and Python scripts. None of the scripts have consistent file extensions (.pl, .perl, .py, .sh, or no extension at all). I need to identify which files are Perl scripts, then add a new line of code to set a variable if the variable is not already in the Perl script.

Thanks to How to insert newline character after comma in `),(` with sed? I came up with this code that uses sed to insert the new line of code.

This code works, but is there a more efficient way to do it?

#! /bin/bash

NL='
$|++'

for f in `find "$1" -type f`
do
    if [ $(grep -cP "^\#\!/.+/perl" "${f}") -gt 0 ]
    then
        if [ $(grep -c "$|" "${f}") -eq 0 ]
        then
            sed -i -r "s/^#!\/(.+)\/perl(.+)$/#!\/\1\/perl\2\\${NL}/g" "${f}"
        fi
    fi
done
  • 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-06-03T18:15:18+00:00Added an answer on June 3, 2026 at 6:15 pm

    You have a number of Useless Uses of Grep -c there. See http://porkmail.org/era/unix/award.html

    #! /bin/bash
    
    NL='
    $|++'
    
    for f in `find "$1" -type f`
    do
        grep -qP "^\#\!/.+/perl" "${f}" &&
        ! grep -q "$|" "${f}" &&
        sed -i -r "s/^#!\/(.+)\/perl(.+)$/#!\/\1\/perl\2\\${NL}/g" "${f}"
    done
    

    The short-circuit && is not an optimization, just a personal preference. You could just as well keep your nested ifs, or perhaps something like

        if grep -qP "^#!/.+/perl" "$f" && ! grep -q "$|" "$f"; then ...
    

    It might be more efficient still to do the first grep (at least) in a sed script since presumably you are only interested in the first line of the script. (On the other hand, why do you have a /g flag on the sed substitution if that is the case?)

    Actually you probably mean

        sed -i -r "1s%^(#!/.+/perl.*)$%\1\\${NL}%" "$f"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My OS environment is linux. I have a php file in this folder -
I have this Perl script for monitoring a folder in Linux. To continuously check
after installing XAMPP on linux i have seen the folder htdocs belongs to nobody.
I have a linux VPS that uses an older version of python (2.4.3). This
I have existing Linux shared object file (shared library) which has been stripped. I
The linux/ folder contains the ip.h header file, wherein part of the structure of
I have a file containing Unicode characters on a server running linux. If I
I have recently taken over a linux box running subversion. The folder where the
I have a problem with my Makefiles on Gentoo Linux. Here is my folder
I have written an (obviously) excellent tool in python (under linux) that I would

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.