how to insert a string as a first line of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to insert a string as a first line of the file
# 1  
Old 12-15-2010
how to insert a string as a first line of the file

Hi
I need to update a number of existing files and insert #!/bin/ksh line a the first line of the file.
Is there any awk or sed command which would help me to do that instead of doing it manually?
Thanks a lot -A
# 2  
Old 12-15-2010
If your sed supports the -i switch, you can do:
Code:
sed -i '1i\#!/bin/ksh' infile

# 3  
Old 12-15-2010
No, -i is not supported by sed Smilie
# 4  
Old 12-15-2010
Then you can do like this:
Code:
sed -i '1i\#!/bin/ksh' infile > temp_file;mv temp_file infile

# 5  
Old 12-15-2010
Or with ed
Code:
<some "ls" command that gets the files you want> | while read FILE; do
  ed $FILE << !
1i
#!/bin/ksh
.
w
q
!
done

# 6  
Old 12-15-2010
Code:
perl -i~ -0777pe's/^/\#\!\/bin\/ksh\n/' file

# 7  
Old 12-15-2010
Thanks a lot guys! It worked
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

2. Shell Programming and Scripting

Insert line based on found string

Hi All I'm trying to insert a new line at the before each comment line in a file. Comment lines start with '#-----' there are other comments with in lines but I don't want a new line there. Example file: blah blah #do not insert here #this is a comment blah #some more #another comment... (10 Replies)
Discussion started by: Mudshark
10 Replies

3. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

4. Shell Programming and Scripting

Awk, if line after string does not match insert

I have a large file with interface records. I need to check every record that has the string "encapsulation bridge1483" and if the next line after this does not have "ip description" then I need to insert a line to add "ip description blah_blah_blah. Sample file: interface atm 1/0.190158... (3 Replies)
Discussion started by: numele
3 Replies

5. UNIX for Dummies Questions & Answers

insert string at end of line if it found from list

Hi all, can some one help me out file 1 i have 06/01 3:14 d378299 06/01 8:10 d642036 06/01 10:51 d600441 06/01 10:52 d600441 06/01 11:11 d607339 06/01 11:49 d398706 something like this and in file named list i have ( there is space btwn 06/01 and 11:49 and d398706) d607339... (5 Replies)
Discussion started by: zozoo
5 Replies

6. Shell Programming and Scripting

How to insert string at particular 4th line in text file

I just want to add a particular string in text file using shell script text file format 1 columns-10 2 text=89 3 no<> 4 5 test-9876 6 size=9 string need to insert in 4th line <switch IP='158.195.2.567' port='5900' user='testc' password='asdfrp' Code='8'> After inserting the... (8 Replies)
Discussion started by: puneet.goel
8 Replies

7. Shell Programming and Scripting

KSH - search for string and insert line above

Hi,, Please excuse my lack of knowledge here. I think I am tring to do something fairly advanced yet am a bit of a beginner..... I want to use a korn shell (as is the norm with others in my company) to write a script to search a file for a string and insert some text above that line. Sudo... (5 Replies)
Discussion started by: HugoDarley
5 Replies

8. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

9. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

Failed to insert string into file before line

Hi folks, I have the following program: #! /bin/ksh set -xv export grepDataSource="</data-sources>" export DB_HOST=tornado export SCHEMA_NAME=IAS10G export dataSource=data-sources.xml sourceString="<data-source class=\"oracle.jdbc.pool.OracleConnectionCacheImpl\" name=\"RiGHTvDS\"... (7 Replies)
Discussion started by: nir_s
7 Replies
Login or Register to Ask a Question