Issue
I am attempting to insert blank line above a line when we match a pattern. We have a grep statement that will find the line and store the line number in a variable.
In this example I want to insert a blank line above line 1 which is the happy
line and then insert a line above line 3 which has the text sad
.
This works fine with a sed command however I want to use variable
substitution for the line number in the sed
statement and that is where it fails. Let me move on and show the example that
I have created to show what issue I am having.
Here is our sed command that works when using it without the variable:
sed '1i\\' test # insert blank line at top of file:
Here is our file named: test
which has 3 lines:
Line1=happy
Line2=mad
Line3=sad
We have two variables for our sed
statement:
1: this has the line of happy - which is 1.
2. this has the line of sad - which is 3.
Here is the variables that we want the sed
statment to use:
h=$(grep -n happy test | cut -d : -f 1)
s=$(grep -n sad test | cut -d : -f 1)
Show that the h
and s
variables seem to work:
user@host:~$ echo $h
1
user@host:~$ echo $s
3
Show that our sed
statement works properly to output a blank line at the beginning of the file - which is line 1 & then also for line 3.
sed '1i\\' test # we test that it outputs a blank line on the top of the file first - without our variable:
user@host:~$ sed '1i\\' test
# here is our blank line.
happy
mad
sad
user@host:~$ sed '3i\\' test
happy
mad
# here is our blank line for line 3.
sad
Now we move onto testing it with our variables defined in the command substitution variables h
and s
so we can try to do the same thing as we did above.
This is where it does not work - I will not test with both variables since it does not work with the first variable. I have tried different syntax that I have researched heavily but cannot get sed
to work with the variable.
sed "$si\\" test # try to insert the blank line with the variable at top of file
user@host:~$ sed '$hi\\' test # we test that it outputs a blank line on the top of the file first - with our variable with ticks ' ':
sed: -e expression #1, char 3: extra characters after command
user@host:~$ sed "$hi\\" test # we test that it outputs a blank line on the top of the file first - with our variable with quotes " " :
sed: -e expression #1, char 1: unterminated address regex
user@host:~$ sed "'$hi\\'" test # we test that it outputs a blank line on the top of the file first - with our variable with quotes/ticks "' '" :
sed: -e expression #1, char 1: unknown command: `''
I have tried several other forms of quotes/ticks ect to try to get it to work. I read around on stack-overflow significanly and found that I should use quotes around my command if using variables.
I have been given a comment to utilize the { } around my variable however when doing this it does not output a line above like it does with the real text:
user@host:~$ sed "${h}i\\" test # the command does not error now but does not output the line either - variable h.
happy
mad
sad
user@host:~$ sed "${s}i\\" test # the command does not error now but does not output the line either - variable s.
happy
mad
sad
user@host:~$ sed '1i\\' test
# blank line here
happy
mad
sad
Solution
This should work perfectly. I tested it on Ubuntu with no issues.
number=3
sed $number'i\\' test.txt
Regards!
Answered By - Matias Barrios Answer Checked By - Mildred Charles (WPSolving Admin)