Adding lines to file through a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding lines to file through a shell script
# 1  
Old 02-26-2009
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. Smilie
# 2  
Old 02-26-2009
Quote:
Originally Posted by Doctor Manhatta
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.
Yes.
Quote:
Any help would be greatly appreciated. Smilie
If you want more help, you'll have to be more specific.
# 3  
Old 02-26-2009
Alright, sorry about that.

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

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

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

Last edited by Doctor Manhatta; 02-26-2009 at 01:46 AM..
# 4  
Old 02-26-2009
Quote:
Originally Posted by Doctor Manhatta
Alright, sorry about that.

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

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

to a specific line in the C file, how would that look in a shell script?
Code:
insert="for( i = 0 ; i < 20 ; i++)
{
scanf("%d",&array[i]);
}"

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".
# 5  
Old 02-26-2009
Thank you very much! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to filter certain lines from a file

hi~ i need script on AIX. and have a text file following : create aa 1 2 3 from a@a; create bb from b; create cc 3 4 5 6 6 7 from c@c; (7 Replies)
Discussion started by: tomato00
7 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

4. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

5. Shell Programming and Scripting

Shell script for adding a table in mysql with 10,000 lines ... pls help

Hi , I am new to shell scripting . I need to write a shell script in sql to add 10,000 lines of data in a table . Pls help guys :) ---------- Post updated at 07:08 PM ---------- Previous update was at 03:40 PM ---------- guys please help !!! (3 Replies)
Discussion started by: vinumahalingam
3 Replies

6. UNIX for Advanced & Expert Users

Concatenate lines in file shell script

Hi colleagues, I have a file in this format. "/cccc/pppp/dddd/ggg/prueba.txt". ERROR" THE error bbbbbbbbbb finish rows. "/kkkk/mmmm/hhhh/jjj/ejemplo.txt". ERROR This is other error rows.I need my file in this format. "/cccc/pppp/dddd/ggg/prueba.txt". ERROR" THE error bbbbbbbbbb finish rows.... (3 Replies)
Discussion started by: systemoper
3 Replies

7. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

8. Shell Programming and Scripting

Need Shell Script to delete lines in a file

Hi All, I have a file with 3 columns (Bank Name, Account Number and Amount). My requirement, I need to delete lines using Unix Shell script: 1. Which are having Alphanumeric characters in Account Number (eg. Line3). 2. Which are having 0.00 in amount. (eg. Line4) 3. And also I need to... (4 Replies)
Discussion started by: phani333
4 Replies

9. Shell Programming and Scripting

Shell script: returning the file with the most lines

Hey I am relatively new to Linux and shell scripting, looking for a spot of help with a script I am working on. I am writing a script that counts the number of lines in all the files in a directory, sorts them by line number and then returns ONLY the file with the most lines. Right now I can... (11 Replies)
Discussion started by: Breakology
11 Replies

10. Shell Programming and Scripting

Shell Script to read specific lines in a file

I have a file with contents as follows Record 1: Rejected - Error on table "DWO"."P2G_CUST_EVENTS". ORA-00001: unique constraint (DWO.CUST_EVENTS_PK) violated Record 5: Rejected - Error on table "DWO"."P2G_CUST_EVENTS". ORA-00001: unique constraint (DWO.CUST_EVENTS_PK) violated Record 6:... (5 Replies)
Discussion started by: varshanswamy
5 Replies
Login or Register to Ask a Question