Insert a line in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert a line in a text file
# 1  
Old 07-31-2008
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
# 2  
Old 07-31-2008
Try this awk solution:
Code:
awk -v n="new line" '/servicegroup_name 24x7-comunicacions/{print $0 "\n" n ;next}1' file

# 3  
Old 07-31-2008
more simple way:

sed '/servicegroup_name 24x7-comunicacions/a\
blabla' filename
# 4  
Old 08-01-2008
thanks

I tested both solutions, and they both works. Thank you!
I like the sed one more, its just less complicated command.
But both output to the screen I need it to write to that same file.
I tried
outputing like
sed '/alias 24x7-comunicacions/a\
blabla' servicegroup-24x7-comm.cfg>new.txt
cp new.txt servicegroup-24x7-comm.cfg

Is that the best way ?

Also I need to pass a variable to that command how do it do??
LINEX=" members saturn,PING"
echo $LINEX
sed '/alias 24x7-comunicacions/a\$LINEX' $FILECFG
that prints $LINEX

Last edited by 4scriptmoni; 08-01-2008 at 05:55 AM..
# 5  
Old 08-01-2008
Quote:
Originally Posted by 4scriptmoni
I like the sed one more, its just less complicated command.
Good guy! ;-))


Quote:
But both output to the screen I need it to write to that same file.
I tried

[...]

Is that the best way ?
Yes, more or less. sed principally does not overwrite the file it works on, so you have to save its results to a temporary file and copy/move the contents over the original afterwards. You can use "mv" instead of "cp", because you don't need a save copy of the results file, but thats a minor issue:

mv (instead of cp) new.txt servicegroup-24x7-comm.cfg

Quote:
Also I need to pass a variable to that command how do it do??
[...]
that prints $LINEX
The reason is, that sed commands might contain characters which have special meaning to the shell. To prevent the shell from being confused we usually surround all sed programs with single quotes, which do exactly this:

sed 'some_commands' file > file.new

Unfortunately this also prevents the special character "$" to be interpreted: An expression like "$variable" is in fact a command to the shell, saying: replace the string "$variable" with the content of the variable "variable". After preventing this interpretation this mechanism doesn't work any more, which is why you get the result "$LINEX" instead of the content of the variable LINEX. Therefore the solution is to find a way to exclude the variable expression from the single-quoted string:

sed '/alias 24x7-comunicacions/a\'"$LINEX" $FILECFG

I hope this helps.

bakunin
# 6  
Old 08-01-2008
thanks

thanks bakunin,
the explanation was perfect.
but i am afraid it didnt work.
I have this inside a loop and I cant make it work Smilie
Because I belive the sed is appending write under the
string: alias 24x7-comunicacions
Or is it because >> dosent work well, or both?
THe result is only the first LINEX

echo "$LINESB" | while read line; do
#COUNTS THE LINES, FIRST LINE IS BAD! THEN ADD "members $HOSTNAME, each line TO maps.txt"
: $((LCOUNT = $LCOUNT + 1))
if [ $LCOUNT -gt 1 ]; then
#echo $LCOUNT
LINEX=" members $HOSTNAME,$line"
echo "$LINEX"
sed '/alias 24x7-comunicacions/a\'"$LINEX" "$FILECFG" >>temp.txt
fi
done
# 7  
Old 08-01-2008
Oops, my fault, sorry.

The backslash is the shells "escape char". It means: "don't interpret any special meaning of the following character, but take it literally. In the original (by freelong) the backslash escaped a newline character:

Code:
sed '...... a\
....'

This newline character you did skip and subsequently i did the same. The escaped character was now the single quote and therefore the single quotes surrounding the sed expression did not match any more. It was like you would have forgotten the closing quote.

bakunin

Last edited by bakunin; 08-01-2008 at 11:38 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert new line of text into Odbc.ini file

I am new to Perl. I wrote a Perl program that inserts text "EnableScrollableCursors=3" after a section of contexts in the odbc.ini file matches a variable in an array list. "EnableScrollableCursors=3" is added to a newline before whitespaces separate each section of contexts in the odbc.ini. ... (0 Replies)
Discussion started by: dellanicholson
0 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. UNIX for Dummies Questions & Answers

Insert a line in a sorted text file(s)

Hello, I am trying to add a line (usually just a word) to some text files in a directory that are already sorted. I just don't want to run the sort command again because it can take a long time when the text or log files are really huge. I have a bashscript that will take in the 1st argument... (7 Replies)
Discussion started by: raptor25
7 Replies

6. 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

7. Shell Programming and Scripting

Sed insert text at first line of empty file

I can't seem to get sed to allow me to insert text in the first line of an empty file. I have a file.txt that is a 0 byte file. I want sed to insert " fooBar" onto the first line. I've tried a few options and nothing seems to work. They work just fine if there's text in the file tho. Help? (4 Replies)
Discussion started by: DC Slick
4 Replies

8. 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

9. Shell Programming and Scripting

how to insert a extra line in a text file

how to insert a extra line in a text file using a sh command iam trying to think of a way to add a extra line but without deleting the whole text do anyone have any ideas (2 Replies)
Discussion started by: bhaviknp
2 Replies

10. 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
Login or Register to Ask a Question