简体   繁体   中英

How to delete lines starting with certain numbers in a file?

Simple question here but I'm kinda stuck.

Let's say I have a file with 20 lines and 4 columns. The first column is a number (1 to 20).

I have an other file with a few numbers in it like this

1
4
19

Now, how can I delete the line (in the first file) starting with the numbers in the second file. My main problem is that if I do a sed, the number 1 will get 10, 11, 12, and on. How can I do this the right way?

Thanks a lot!

EDIT: examples

file1

1       a       a       a
2       b       b       b
3       c       c       c
4       d       d       d
5       e       e       e
6       f       f       f
7       g       g       g
8       h       h       h
9       i       i       i
10      j       j       j
11      k       k       k
12      l       l       l
13      m       m       m
14      n       n       n
15      o       o       o
16      p       p       p
17      q       q       q
18      r       r       r
19      s       s       s
20      t       t       t

file2

1
4
19

the result I want:

2       b       b       b
3       c       c       c
5       e       e       e
6       f       f       f
7       g       g       g
8       h       h       h
9       i       i       i
10      j       j       j
11      k       k       k
12      l       l       l
13      m       m       m
14      n       n       n
15      o       o       o
16      p       p       p
17      q       q       q
18      r       r       r
20      t       t       t

You can use awk for this:

awk 'FNR==NR{a[$1]; next} !($1 in a)' file2 file1
2       b       b       b
3       c       c       c
5       e       e       e
6       f       f       f
7       g       g       g
8       h       h       h
9       i       i       i
10      j       j       j
11      k       k       k
12      l       l       l
13      m       m       m
14      n       n       n
15      o       o       o
16      p       p       p
17      q       q       q
18      r       r       r
20      t       t       t

Breakup of awk command :

FNR == NR {                  # While processing the file2
  a[$1]                      # store the 1st field in an array
  next                       # move to next record
}
                             # while processing the file1
!($1 in a)                   # print a row from file1 if 1st field is not in array 'a'

You can use sed to create a sed script that deletes the given lines:

 sed 's=^=/^=;s=$=\\s/d=' numbers

It creates the following sed script:

/^1\s/d
/^4\s/d
/^19\s/d

Ie delete the line if it starts with a 1, 4, or 19, followed by whitespace.

You can directly pipe it to sed to run it:

sed 's=^=/^=;s=$=\\s/d=' numbers | sed -f- input-file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM