Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

eg ;

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 record of that particular line;

col1_id,col2,col3,col4,col5
1000541,aaaa,bbbb,cccc,dddd
1000543,iiii,jjjj,kkkk,llll
1000544,mmmm,nnnn,oooo,pppp
share|improve this question
1  
grep -v 1000542 –  Stéphane Chazelas Jun 18 at 12:17

2 Answers 2

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.

share|improve this answer
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. –  Stéphane Chazelas Jun 18 at 12:17
    
Sorry, the description wasn't clear enough. I edited the answer. –  psimon Jun 18 at 12:26

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.

share|improve this answer
    
Why not simply awk '!/1000542/' or grep -v 1000542? –  Stéphane Chazelas Jun 18 at 12:19
    
There are more number of possiblities for an text processing question :-) –  Avinash Raj Jun 18 at 12:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.