简体   繁体   中英

Delete a single line from a file with a unix shell script

I'm working with a simple shell script that needs to delete a single line from a file if a string entered at command line is found. Instead it deletes the entire contents of the file.

the data file is like this(each entry is on a new line):

Name:Address:Phone:email
Joshua Brown:42404 Transport Center:380-(605)964-4223:jbrown0@desdev.cn
Margaret Elliott:24 Forest Plaza:48-(269)541-4040:melliott1@toplist.cz
Gary Flores:76868 Westport Drive:86-(478)534-8313:gflores2@reference.com

my code:

#assuming $1 is the pattern that needs to be searched for
#if found, delete the entire line
sed -i '/$1/d' filename
sed  -i 'Nd' filename

N表示行号,从1开始

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