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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert string at particular 4th line in text file
# 1  
Old 08-05-2009
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 output of file looks like

1 columns-10
2 text=89
3 no<>
4 <switch IP='158.195.2.567' port='5900' user='testc' password='asdfrp' Code='8'>
5 test-9876
6 size=9

Please suggest..
# 2  
Old 08-05-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

****************************************************

Code:
$> STRING="<switch IP='158.195.2.567' port='5900' user='testc' password='asdfrp' Code='8'>"
$> awk -v string="${STRING}" 'NR == 4 {print string} $0' infile
columns-10
text=89
no<>
<switch IP='158.195.2.567' port='5900' user='testc' password='asdfrp' Code='8'>
test-9876
size=9


Last edited by zaxxon; 08-05-2009 at 09:50 AM.. Reason: slight modification, the single quotes were gone
# 3  
Old 08-05-2009
Code:
awk '
NR == 4 { $0="<switch IP='158.195.2.567' port='5900' user='testc' password='asdfrp' Code='8'>" 
            }
            { print $0 }  
' textfile

# 4  
Old 08-05-2009
@kshji
This way the single quotes ' are gone. Had the same problem in my pre-edited solution.
# 5  
Old 08-05-2009
It's still not working.. As there already a blank line in 4th position

Error I am getting : awk: can't set $0
record number 4
# 6  
Old 08-05-2009
Which solution did you try out? I edited mine meanwhile.
# 7  
Old 08-05-2009
Straight forward I am executing the below scripts but it is generating the below error

STRING="<switch IP='158.195.2.567' port='5900' user='testc' password='asdfrp' Code='8'>"

awk -v string="${STRING}" 'NR == 4 {print string} $0' test_file


awk: syntax error near line 1
awk: bailing out near line 1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert text after the first occurance of searched string entry in a file

My server xml file has huge data part of which i'm sharing below. I wish to add the below text held by variable "addthisline" after the closing braces i.e --> once the first </Connector> tag is found. addthisline="I need to be inserted after the comments" Thus my searchstring is... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

3. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

4. UNIX for Dummies Questions & Answers

Insert a line in a text file

I want to insert a line with text after the 9th line of a text file. How would I do this using sed or awk? (2 Replies)
Discussion started by: lost.identity
2 Replies

5. Shell Programming and Scripting

Using sed to insert text file at first line

sed '1r file.txt' <source.txt >desti.txt This example will insert 'file.txt' between line 1 and 2 of source.txt. sed '0r file.txt' <source.txt >desti.txt gives an error message. Does anyone know how 'sed' can insert 'file.txt' before the first line of source.txt? (18 Replies)
Discussion started by: psve
18 Replies

6. Shell Programming and Scripting

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 (10 Replies)
Discussion started by: aoussenko
10 Replies

7. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

8. Shell Programming and Scripting

Insert a line in a text file

So I need to write lines into line X of file X. I can get the file by doing: cfgnumber=$(cat -n -comm.cfg| grep -i "servicegroup_name 24x7-comunicacions") echo $cfgnumber it outputs the Line where it finds now I need to start writing something right bellow that line. thanks (10 Replies)
Discussion started by: 4scriptmoni
10 Replies

9. Shell Programming and Scripting

Insert text file at a certain line.

I need to insert a file called temp_impact (which has about 15 lines in it) to a file called 11.23cfg starting at line 33. I searched the forums and found the sed '34i\ test' 11.23cfg > newfile That will enter word test at the appropriate line, but i need the entire file dumped there. Any... (4 Replies)
Discussion started by: insania
4 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