Created
November 20, 2011 01:40
-
-
Save c00kiemon5ter/1379674 to your computer and use it in GitHub Desktop.
add a line to a file, on the first comment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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