0

I have a .csv file having several lines of data. I have to search for a particular value in the string/record and delete that line itself.

For example:

abc.csv

col1_id,col2,col3,col4,col5
1000541,aaaa,bbbb,cccc,dddd
1000542,eeee,ffff,gggg,hhhh
1000543,iiii,jjjj,kkkk,llll
1000544,mmmm,nnnn,oooo,pppp

I need to search for 1000542 and remove the whole line containing that number:

col1_id,col2,col3,col4,col5
1000541,aaaa,bbbb,cccc,dddd
1000543,iiii,jjjj,kkkk,llll
1000544,mmmm,nnnn,oooo,pppp
1
  • 1
    grep -v 1000542 Commented Jun 18, 2014 at 12:17

2 Answers 2

3

You can use sed to remove one line:

sed -i '/1000542/d' abc.cvs

where d removes the line, -i tells sed to operate on the file rather than just output the modified text, 1000542 is the pattern you want to search for and abc.cvs is the filename.

2
  • 1
    It's d that removes the line. -i is a GNU extension that's for in-place editing (making sed a text file editor instead of a stream editor). With the sed of recent BSDs you need -i '' instead. Portably, you can use ed or perl. Commented Jun 18, 2014 at 12:17
  • Sorry, the description wasn't clear enough. I edited the answer. Commented Jun 18, 2014 at 12:26
0

You could use awk also,

awk '/1000542/ {next}1' infile > outfile

Example:

$ awk '/1000542/ {next}1' file
col1_id,col2,col3,col4,col5
1000541,aaaa,bbbb,cccc,dddd
1000543,iiii,jjjj,kkkk,llll
1000544,mmmm,nnnn,oooo,pppp

It skips the line which contains 1000542 from printing.

2
  • Why not simply awk '!/1000542/' or grep -v 1000542? Commented Jun 18, 2014 at 12:19
  • There are more number of possiblities for an text processing question :-) Commented Jun 18, 2014 at 12:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.