Adding lines to file through a shell script

Hi, I'm pretty new to the whole scripting thing. I've gotten a decent hang of it so far but am wondering if there's a way to add lines to a C, C++, Java, HTML, text file with a shell script.

Any help would be greatly appreciated. :slight_smile:

Yes.

If you want more help, you'll have to be more specific.

Alright, sorry about that.

As an example, if I had a C file called "number.c" and wanted to add in the lines

for( i = 0 ; i < 20 ; i++)
{
       scanf("%d",&array);
}

to a specific line in the C file, how would that look in a shell script?

insert="for( i = 0 ; i < 20 ; i++)
{
scanf("%d",&array);
}"

awk -v insert="$insert" '
 {print}
 SELECTOR { print insert }
' "$FILE" > tempfile && mv tempfile "$FILE"

SELECTOR is a pattern that matches the line in the file where you want to insert the new lines.

It could be NR == 10 if you want to insert after line 10.

Or /INSERT HERE/ to insert after a line containing "INSERT HERE".

Thank you very much! :smiley: