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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:51:36+00:00 2026-06-14T13:51:36+00:00

Possible Duplicate: Sed/Awk to search and replace/insert text in files I would like to

  • 0

Possible Duplicate:
Sed/Awk to search and replace/insert text in files

I would like to know how to add one “header” line into multiple text files contained in one directory. Bash command line would be great!

Thx.

EDIT

I found my needs in here: http://perldoc.perl.org/index-faq.html enjoy!
Here is my answer:

perl -pi -e 'print "**MyHeaderText**\n" if $. == 1' *
  • 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-14T13:51:37+00:00Added an answer on June 14, 2026 at 1:51 pm

    Option 1: Works pretty much any version of Unix

    tmp=$(mktemp)   # Create a temporary file
    trap "rm -f $tmp; exit 1" 0 1 2 3 13 15
    
    header="This is the header line to be inserted"
    
    for file in "$@"
    do
        {
        echo "$header"
        cat $file
        } > $tmp
        mv $tmp $file
    done
    
    rm -f $tmp
    trap 0
    

    This creates a temporary file securely and makes sure it gets removed under a reasonable collection of signals (HUP, INT, QUIT, PIPE and TERM). The main loop then copies the header string and the file to the temporary, and moves the temporary over the original, removes any leftover file (in case something went wrong) and cancel the cleanup so the shell can exit cleanly.

    If your original file had multiple (hard) links, or if it was a symlink, you lose these special properties. To fix that, you have to use cp $tmp $file, and then you have to remove the file in $tmp too.

    If you don’t have the mktemp comand, you can use:

    tmp=${TMPDIR:-/tmp}/ins.$$
    

    to generate a name. It is more easily predictable than the name from mktemp and less secure, especially if you’re running as root. There might also be wisdom in using the current directory as $TMPDIR if you have multiple file systems.

    Option 2: GNU sed

    If you have GNU sed and you don’t have symlinks or hard links to deal with, then you can use its -i option to do an in-place alter.

    header="This is the line to be inserted"
    
    for file in "$@"
    do
        sed -i -e "1i\
    $header" $file
    done
    

    This inserts the value of $header before the first line of each file. You could write the edit script into a file and use sed -i -f sed.script $file to avoid awkward indentation in the loop.

    Option 3: Other tools

    There are many other possible techniques. For example, you could use ed or ex to edit the file. You could use Perl or Python or awk to do the processing.

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

Sidebar

Related Questions

Possible Duplicate: C# driver development? I would like to know if I can do
Possible Duplicate: eliminate unwanted output using awk and sed The following contents are there
Possible Duplicate: Use slashes in sed replace I need to find the following string
Possible Duplicate: Sed command find and replace in file and overwrite file doesnt work,
Possible Duplicate: Truncate stdin line length? I have been looking for an awk or
Possible Duplicate: Join lines based on pattern I have the following file: test one
Possible Duplicate: Shell script to append text to each file? How to append output
Possible Duplicate: How do I create a directory and parent directories in one Perl
Possible Duplicate: Difference Between Equals and == in which cases equals() works exactly like
Possible Duplicate: array_splice() for associative arrays How to add an array value to the

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.