sed delete option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed delete option
# 1  
Old 05-01-2011
sed delete option

I have tried doing this to delete some lines:
Code:
sed '1,10d' file

Now I want to specify a variable as a line number for example:

lastline=wc -l file
linestart=$lastline - 20

sed '$linestart,$lastlined' file

but this will give error:
Code:
sed: -e expression #1, char 3: extra characters after command

Is there anyway to place a variable as the line number?

Last edited by Yogesh Sawant; 05-01-2011 at 05:09 AM.. Reason: added code tags
# 2  
Old 05-01-2011
Code:
tail -r infile | tail +21 | tail -r

Code:
tail -r infile | tail -n +21 | tail -r

Code:
$ cat tst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ tail -r tst | tail +21 | tail -r
1
2
3
4
5
6
7
8
9
10

---------- Post updated at 09:32 AM ---------- Previous update was at 09:28 AM ----------

Code:
$ lastline=$(wc -l yourfile)
$ awk -v c="$lastline" 'NR<=c-20' yourfile
1
2
3
4
5
6
7
8
9
10

# 3  
Old 05-01-2011
Use double quotes
Code:
sed "$start,$end d" file

Or, if you have metacharacters in there, use single quotes and turn them on and off to get the $start and $end in:
Code:
 sed ''$start','$end' d' file

The same way you can sneak in variables into regex's.
This User Gave Thanks to mirni For This Post:
# 4  
Old 05-01-2011
Code:
$ lastline=$(( $(wc -l <tst)-20 ))
$ sed '1,'$lastline'!d' tst
1
2
3
4
5
6
7
8
9
10

This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 05-01-2011
Thanks mirni. Your code works very well. You the best.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed not applying /d "delete line" option

So I'm on an AIX machine. And sed is not applying /d "delete line" option when I also include match word options \< and \> examples... echo cat | sed '/\<cat\>/d'will return cat for some reason echo cat | sed "/\<cat\>/d"will also still return cat. Of course i can just run echo cat... (9 Replies)
Discussion started by: escooter87
9 Replies

2. Shell Programming and Scripting

Rsync with delete option

Hi, I have restored backup of a directory on one server to another server. But there may be some differences because the date on which backup was taken is 5 days back, so I want to delete the files on target if something is not in sync, but do not want to copy it all, since this is already... (1 Reply)
Discussion started by: solaris_1977
1 Replies

3. Shell Programming and Scripting

sed -n option

Hi i am facing problem with sed -n option could you please help me on this, i have a file test the contents of the file is width="75">10/0 4/12</td>^M><a href='courtorders/100412zr.pdf' target="_blank">Miscellaneous Order</a></td>^M width="75">10/01/12</td>^M><a href='courtorde... (8 Replies)
Discussion started by: ragilla
8 Replies

4. Shell Programming and Scripting

sed with -e option

sed "s/^/8,A1,$dat,id2_3,/g" -e sed "s/$/,,,,,,,,,,,/g" temporary wn m running this script m getting a error... plz help me with this.... O/p sed: -e expression #1, char 3: unterminated `s' command ---------- Post updated at 11:35 AM ---------- Previous update was at 11:33 AM... (7 Replies)
Discussion started by: nikhil jain
7 Replies

5. Shell Programming and Scripting

sed 'b' option

Hi, What does the following command do on files? sed '1b; $d' To me it just deletes the last line of the file. If so what is the significance of sed's 'b' option? Thanks (1 Reply)
Discussion started by: royalibrahim
1 Replies

6. UNIX for Dummies Questions & Answers

sed -i option example

Can anyone give detailed and example for sed -i option. Where can we use this option?:) (3 Replies)
Discussion started by: gwgreen1
3 Replies

7. Solaris

option to delete .tar file while extracting

Is there an option in tar which deletes the .tar file as soon as it is successfully extracted. (5 Replies)
Discussion started by: vickylife
5 Replies

8. UNIX for Dummies Questions & Answers

rsync with the --delete option

Tell me this - set me straight! The --delete option says "delete files that don't exist on the sending side" Does this mean and only mean that it will delete files from the DESTINATION that DON'T EXIST on the sending side? :confused: (1 Reply)
Discussion started by: sallender
1 Replies

9. UNIX for Advanced & Expert Users

rsync with the --delete option.

Tell me this - set me straight! the --delete option says "delete files that don't exist on the sending side" Does this mean and only mean that it will delete files from the DESTINATION that DON'T EXIST on the sending side? :confused: (2 Replies)
Discussion started by: sallender
2 Replies

10. UNIX for Dummies Questions & Answers

sed option to delete two words within a file

Could someone please help me with the following. I'm trying to figure out how to delete two words within a specific file using sed. The two words are directory and named. I have tried the following: sed '//d' sedfile sed '//d' sedfile both of these options do not work..... ... (4 Replies)
Discussion started by: klannon
4 Replies
Login or Register to Ask a Question