Visit Our UNIX and Linux User Community


Using SED to delete some lines from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using SED to delete some lines from file
# 1  
Old 09-30-2009
Using SED to delete some lines from file

Hi All,

I have one /etc/hosts.equiv file which has following entries
Code:
##########
wcars42g        admin
wcars42b        netmgr
wcars42b        oemssrvr
wcars42f        admin
wcars42f        netmgr
wcars42f        oemssrvr
##########

I am trying to delete lines starting from wcars42b. For this
Code:
i=`cat  -n /etc/hosts.equiv | grep wcars42b > counter ; cat counter  | grep  wcars42b | awk '{print $1}'`

If I do
Code:
#echo $i

then I get ouput as --> 2 3. So, I can conclude as , i is an array with two elements viz. 2 3

Now, If I try to use following command to delete the lines
Code:
sed ''$i','$i' d' /etc/hosts.equiv.test > /temp1

then I got error saying command not recognised. This is because first value provided to sed should be starting line number and second value should be ending line number.

Can anybody tell me, how should I provide 2 as starting line number and 3 as ending line number?

Last edited by Franklin52; 09-30-2009 at 07:42 AM.. Reason: Please use code tags!
# 2  
Old 09-30-2009
Not sure if I got it but basically:

Code:
grep -v ^wcars42b infile
#or
sed '/^wcars42b/d' infile

Shell variables can't be substituted inside ' and '. Use " and " instead for example. Or write it like
Code:
sed '/'${i}'/d' infile

Next time use [code] and [/code] tags when posting code, data or logs please.

Last edited by zaxxon; 09-30-2009 at 07:25 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #684
Difficulty: Medium
According to both NetMarketShare and WikiMedia, Safari claimed a mobile market share of over 40% in 2019.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

2. Shell Programming and Scripting

How to delete lines using sed?

<VirtualHost 192.168.1.158:80> DocumentRoot /home/ten ServerName ten.com </VirtualHost> <VirtualHost 192.168.1.158:80> DocumentRoot /home/sachin ServerName sachin.com </VirtualHost> <VirtualHost 192.168.1.158:80> DocumentRoot /home/yuvraj ServerName yuvraj.com... (5 Replies)
Discussion started by: tkmmelvin
5 Replies

3. Shell Programming and Scripting

copy, then delete lines in file with sed using a pattern

I need to copy lines to a new file from files with sed using a pattern in char postions 1-3. Then after the copy, I need to delete those same lines from the input files. For example, string "ABC" in pos 1-3 (6 Replies)
Discussion started by: laksjfhoius9123
6 Replies

4. Shell Programming and Scripting

sed to delete lines from a file by using wildcard

I have a text file with tons of data and I want to remove all lines which are have "240" regardless of ABC or BCD and shouldnt delete anything else ABC_10_00024045.zip blah blah ABC_10_00024045.zip.new ABC_10_00024046.zip blah blah ABC_10_00024046.zip.new ABC_10_00024446.zip blah blah... (3 Replies)
Discussion started by: gubbu
3 Replies

5. Shell Programming and Scripting

Sed to delete lines that with the following

Hi, I'm very new to Sed and I have a very large file that contains data in the following way (*064) 1 4 10 (*064) simulation time = 0.12000E-05 (*064) 1 2 10 (*064) 1 3 10Essentially what I want to do it delete every line that starts with '(*064) 1'I tried the following, ... (2 Replies)
Discussion started by: lost.identity
2 Replies

6. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

7. Shell Programming and Scripting

sed delete pattern skipping first n lines of file.

I have files of more than 10K lines that I need to delete lines that contain a pattern, but I want to keep the first few lines intact. Can this be done with sed? (7 Replies)
Discussion started by: tkg
7 Replies

8. Shell Programming and Scripting

Delete lines containing text with sed

hello all I have bunch of files containing lines of text that surrounding by <# .......#> tags I like to delete this lines from the text files whiteout open the files , can it be done with sed ? or other unix tool (perl mybe )? (2 Replies)
Discussion started by: umen
2 Replies

9. Shell Programming and Scripting

sed help - delete last 2 lines.

I have been reading through the sed one liners, trying to understand what is happening. # delete the last 2 lines of a file sed 'N;$!P;$!D;$d' The above will delete the last 2 line of a file. I tried analyzing what happens. And I got lost :( This is what I understood so far from the... (2 Replies)
Discussion started by: vino
2 Replies

10. Shell Programming and Scripting

Delete multiple lines w/ sed

Hi all, I am trying to figure out the syntx to delete multiple lines w/ sed. I know the following syntax will delete lines 1 THROUGH 5 from filex: sed 1,5d filex But I wan to delete lines 1 AND 5 (keeping lines 2,3, and 4). Does anyone know how to do this in a single sed statement? ... (2 Replies)
Discussion started by: bookoo
2 Replies

Featured Tech Videos