Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: Insert line into source code file after initial comments

Tags:

bash

shell

I need to use bash to insert a line into a python file. This line needs to appear after any initial comments in the the file.

So given the file:

#!/usr/bin/python
# This is just 
# an example comment

moo = "cow"
... etc ...

I need a bash command to insert a new line like this:

#!/usr/bin/python
# This is just 
# an example comment
NEW LINE GOES HERE

moo = "cow"
... etc ...

I am entirely stumped on how to do this. I have tried looping over the file line by line, but that just ends up being pretty horrific and severely messing up the file's whitespace.

Any suggestions would be great!

Adam

PS. Yes, this is a bit of a weird thing to do, it is for part of a continuous integration build script.


Edit

For the record, the code I was trying was:

insert_setup_code() {
    installed=false
    tmpfile="/tmp/$RANDOM"

    cat "$INSTALL_TO" | while read -d \n l; do
        echo "$l" >> $tmpfile
        if [[ ! $installed && ! `echo "$l" | grep "^#"` ]]; then
            echo "LINE OF CODE HERE" >> $tmpfile
            installed=true
        fi
    done
}
like image 409
Adam Charnock Avatar asked Nov 17 '25 20:11

Adam Charnock


2 Answers

I would write:

line="NEW STUFF HERE"
awk -v text="$line" '!/^#/ && !p {print text; p=1} 1' file

The first non-comment line will trigger the block to print the line:

  • !/^#/ -- line does not start with a hash
  • !p -- variable p is not true
like image 166
glenn jackman Avatar answered Nov 19 '25 08:11

glenn jackman


there you go

my addline script. add newline after any initial comments in the filein and write to fileout

#!/usr/bin/env bash

newline="$1"
filein="$2"
fileout="$3"
added=0

while read -r; do
    if ! ((added)) && ! [[ $REPLY =~ ^# ]]; then
        printf "%s\n" "$newline" >> "$fileout"
        ((added++))
    fi
    printf "%s\n" "$REPLY" >> "$fileout"
done < "$filein"

Use as:

$ bash addline "my new line" "readThisFile" "writeToThisFile"

adjust to your needs :)


example usage to itself:

$ bash addline "# a test comment line" addline foo
$ cat foo

#!/usr/bin/env bash
# a test comment line

newline="$1"
filein="$2"
fileout="$3"
added=0

while read -r; do
    if ! ((added)) && ! [[ $REPLY =~ ^# ]]; then
        printf "%s\n" "$newline" >> "$fileout"
        ((added++))
    fi
    printf "%s\n" "$REPLY" >> "$fileout"
done < "$filein"

Updated faster version:
using wc -l and sed to write the rest of the file instead of looping through each line

#!/usr/bin/env bash

newline="$1"
filein="$2"
fileout="$3"
counter=0

while read -r; do
    ((counter++))
    if ! [[ $REPLY =~ ^# ]]; then
        printf "%s\n" "$newline" "$REPLY" >> "$fileout"
        break
    fi
    printf "%s\n" "$REPLY" >> "$fileout"
done < "$filein"

sed -n "$counter,$(wc -l < "$filein")p" "$filein" >> "$fileout"

works as before/above

like image 20
c00kiemon5ter Avatar answered Nov 19 '25 09:11

c00kiemon5ter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!