10

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
2
  • 3
    What make the line you want to delete different with other lines?
    – cuonglm
    Jul 7, 2014 at 18:21
  • modified the question. Check it out.
    – Duck
    Jul 7, 2014 at 18:27

7 Answers 7

21

The easiest way is using grep -v command:

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

0
10

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
4
  • 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, 2014 at 1:29
  • @user13107 in some versions you have to say -i'' for that though
    – SztupY
    Jul 8, 2014 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, 2014 at 13:16
  • 1
    @MvG: missed the space between -i and ''
    – SztupY
    Jul 8, 2014 at 13:40
6

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
    
5

in-place with ed (on the bash prompt)

ed -s file <<<$'/Products below/d\nw'

Or as don crissti recommends:

ed -s file <<<$'/Products below/d\nw\nq'
2
  • 2
    ed answers always deserve a +1
    – evilsoup
    Jul 8, 2014 at 9:28
  • 1
    seconding @evilsoup ; though if you want to go by the book you should add the quit subcommand too: $'/Products below/d\nw\nq' Mar 27, 2015 at 18:30
3

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
3

perl oneliner:

perl -ne 'print unless (m/Products below/') FILE
2

Here is yet another example of using sed:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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