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.

Somewhere in the middle of my CSV file is this line:

Products below this line are out of stockNumber, month, year, reference, store

Note:Number, month, year, reference and store are the CSV fields.

How do I delete this line from the file using a command line command?

Note the CSV is like this

Number, month, year, reference, store
1,1,2014,13322,main
2,2,2014,13322,main
3,3,2011,1322,main
4,4,2012,3322,main
5,4,2013,122,secondary
Products below this line are out of stockNumber, month, year, reference, store
12,411,2010,122,Albany
25,41,2009,122,Dallas
35,24,2008,122,New
share|improve this question
3  
What make the line you want to delete different with other lines? –  cuonglm Jul 7 at 18:21
    
modified the question. Check it out. –  SpaceDog Jul 7 at 18:27

7 Answers 7

up vote 15 down vote accepted

The easiest way is using grep -v command:

grep -v "Products below" your_file.csv > new_file.csv

share|improve this answer
    
worked like magic. Thanks!!!!!! –  SpaceDog Jul 8 at 7:48

With your input data, you can try:

$ sed '/^Products/d' file 
Number, month, year, reference, store
1,1,2014,13322,main
2,2,2014,13322,main
3,3,2011,1322,main
4,4,2012,3322,main
5,4,2013,122,secondary
12,411,2010,122,Albany
25,41,2009,122,Dallas
35,24,2008,122,New

Using sed -i.bak to edit the file inplace and create a backup file:

sed -i.bak '/^Products/d' file
share|improve this answer
    
with certain versions of sed you don't need to do -i.bak just -i will do (but of course w/o backup file) –  user13107 Jul 8 at 1:29
    
@user13107 in some versions you have to say -i'' for that though –  SztupY Jul 8 at 9:26
    
@SztupY: In -i'' the shell should strip the '' and leave you with -i alone. At least if it is a standard bourne-like shell. –  MvG Jul 8 at 13:16
1  
@MvG: missed the space between -i and '' –  SztupY Jul 8 at 13:40

If we can safely assume that you want to delete all lines that start with Products (including the space after the 1st word), these will all work:

  • awk

    awk '$1!="Products" file > newfile
    
  • perl

    perl -ne 'print unless /^Products/' file > newfile
    

    or

    perl -ane 'print if $F[0]!="Products"' file > newfile
    

    or, to edit the file in place

    perl -i -ne 'print unless /^Products/; ' file
    perl -i -ane 'print if $F[0]!="Products"' file 
    
  • grep (this is just a shorter version of svq's answer)

     grep -v ^Products file > newfile
    
  • bash (just for the fun of it)

    while read line; do [[ $line =~ ^Products ]] || echo $line; done <  file > newfile
    
share|improve this answer

in-place with ed (on the bash prompt)

ed -s file <<<$'/Products below/d\nw'
share|improve this answer
1  
ed answers always deserve a +1 –  evilsoup Jul 8 at 9:28

You can use sed to search pattern to delete each line what contain this pattern

sed -i '/Products below this line are out of stockNumber, month, year, reference, store/d' file
share|improve this answer

Here is yet another example of using sed:

sed '/^Products below this line are out of stockNumber, month, year, reference, store$/d' originalDocument > newDocument
share|improve this answer

perl oneliner:

perl -ne 'print unless (m/Products below/') FILE
share|improve this answer

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.