Hi guys ,
I m writing a script to delete a line at particular location.
But i m unable to use variable for specifying line number.
for example.
sed -n '7!p' filename
works fine and deletes 7th line from my file
but
sed -n '$variable!p' filename
gives following error.
sed: -e expression #1, char 3: extra characters after command
clx
June 15, 2010, 6:14am
2
use double quotes instead when using shell variables.
1 Like
+
i tried
sed -n "$variable!p" vm.cfg
and
sed -n '"$variable"!p' vm.cfg
None of them are working.
Please help.
[house@leonov] cat in.file
1st line
2nd line
3rd line
[house@leonov] var=2; sed "${var}d" in.file
1st line
3rd line
1 Like
zaxxon
June 15, 2010, 6:38am
5
Another way:
sed ''${variable}'d' vm.cfg
^^
This is two single quotes, not a double!
1 Like
Scott
June 15, 2010, 6:45am
6
You don't really need quotes at all.
$ cat file1
a
b
c
d
$ X=3
$ sed ${X}d file1
a
b
d
1 Like
Thanks for your input . But now i would like to enter a line at specific line number .
How would i establish this using sed.
Like this
# cat file
12
13
14
15
# sed '/12/d' file
13
14
15
zaxxon
June 15, 2010, 7:06am
9
Like this?
sed '4s/.*/this is the new line\n&/' infile
one
two
three
this is the new line
four
five
Does it have to be sed?
# cat in
1
2
3
4
# export n=3
# export s="aaa"
# awk -vn=$n -vs=$s 'NR==n{print s}1' in
1
2
aaa
3
4
Scott
June 15, 2010, 7:16am
11
What do you mean by "enter a line"? Insert a new line, or?
$ cat file1
a
b
c
d
$ sed '3 i\
new line goes here' file1
a
b
new line goes here
c
d
# or
$ sed '3 a\
new line goes here' file1
a
b
c
new line goes here
d
insert variable $line at line 3:
sed "3s/^/$line\n/" infile
Hi ,
#!/usr/bin/perl
while (<>) {
if ($. == $linenum ) { print "test line \n"; }
print $_;
}
OR
#!/bin/sh
linecount=`cat test1.txt | wc -l`
linetoinsert="test line to insert"
linenumber=4
linecount_head=`expr $linenumber - 1`
linecount_tail=`expr $linecount - $linenumber + 1`
head -$linecount_head test1.txt;echo $linetoinsert;tail -$linecount_tail test1.txt