Skip to content

Instantly share code, notes, and snippets.

@c00kiemon5ter
Created November 20, 2011 01:40
Show Gist options
  • Select an option

  • Save c00kiemon5ter/1379674 to your computer and use it in GitHub Desktop.

Select an option

Save c00kiemon5ter/1379674 to your computer and use it in GitHub Desktop.
add a line to a file, on the first comment
#!/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"
## or just use awk
#
# line="NEW STUFF HERE"
# awk -v text="$line" '!/^#/ && !p {print text; p=1} 1' file
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment