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

Try this awk solution:

awk -v n="new line" '/servicegroup_name 24x7-comunicacions/{print $0 "\n" n ;next}1' file

more simple way:

sed '/servicegroup_name 24x7-comunicacions/a\
blabla' filename

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

Good guy! ;-))

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

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

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 :frowning:
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

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:

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

sorry I got lost.
so what syntax should i use?

sed 'alias 24x7-comunicacionsa\'"$LINEX" "$FILECFG" >>temp.txt

Notice the difference between the two command lines, both from your own post. The first one is correct, the second one isn't:

The "\" character in the second one escapes the character "$", while in the first one it escapes (correctly) the newline character, which causes line to be continued in the next line. This character is not printable, so it is difficult to understand the concept at first. Correct is:

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

Its like this: Think of

X
X

basically as the character sequeence

X<newline-char>X

and this newline-character (usually written as "\n", like the C expression which causes a newline character to be printed) has to be escaped so that sed doesn't think the command line ends there but takes this newline as part of the string to add.

I hope this clarifies things.

bakunin

thanks, but still not working...
sed '/alias 24x7-comunicacions/a\'"$LINEX" $FILECFG >>temp.txt

It keeps doing only the first value of $LINEX variable. Maybe I should append all lines to LINEX string + line break character of all text I want to insert then append just one time.
Inside the loop its behaving strangely...

Yes, this is the right way to do it:

sed '/some_search_pattern/a\
line_to_add\
one_more_line_to_add\
another_line_to_add' /path/to/source > /path/to/result

If you want this to be a variable:

typeset LINEX='line_to_add\
one_more_line_to_add\
another_line_to_add' /path/to/source'

sed '/some_search_pattern/a\
'"$LINEX" /path/to/source > /path/to/result

I hope this helps.

bakunin