I have eight file's which start with: SEALS-*

Each file has a header and data:

#Col_1\Col_2\Col_3\col_4    --- header
\\\\\\\\\\                  --- data 

If it is a null entry, I need to delete this record; it has only \'s on the line.

closed as unclear what you're asking by roaima, jasonwryan, Stephen Kitt, Anthon, cuonglm May 20 '15 at 8:55

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • If i have null in any file i will get only Header and " \\\\\\\ " as a data column which i need to delete that 1 line alone. – deepuu May 20 '15 at 5:36
  • find -type f -exec sh -c '[[ $(sed -n $= $0) -eq 2 ]]' {} \; -exec sed -i '1!d' {} + tried with this which delets 2nd line even it has data, but i need to delete if it has only \\\\\\\\\\\ – deepuu May 20 '15 at 5:36
  • 1
    Welcome to SE. Please add your updates into the Question. If you leave them as comments they can get lost in noise. Please would you also format your question text and include examples of "before" and "after" data files. – roaima May 20 '15 at 7:25

Use the Perl's i flag for in-place edits:

$ perl -pi -e '$_ = "" if m{^\\\\}' file.txt

And if you want to pipe the output of find to it, use xargs:

$ find . -type f | xargs perl -pi -e '$_ = "" if m{^\\\\}'

When using find's -exec instruction:

$ find . -type f -exec perl -pi -e '$_ = "" if m{^\\\\}' {} \;
  • Thanks much guys this worked out find -name "SALE-*" -type f -exec sh -c '[[ $(sed -n $= $0) -eq 2 ]]' {} \; -exec sed -i '/^[\\]*$/d' {} + – deepuu May 20 '15 at 9:12

First answer :

For all that is text manipulation, Perl is always a good possibility :

perl -e 'while(<>){print if(!/(\\\\)+/)}' file > file.new

But if you only want to use the shell, this could work too :

grep -v '\\\\' file > file.new

It works well :)

Edit :

If you can't create a new file, Perl can still do that :

perl -e 'open FILE_R, "+<$ARGV[0]"; @line=<FILE_R>; open FILE_W,"+>$ARGV[0]"; foreach(@line){print FILE_W if(!/(\\\\)+/);}' file

Let me explain this long line :

open FILE_R, "+<$ARGV[0]"; @line=<FILE_R>;

=> Open your file on "read" mode, and copy all data into an array

open FILE_W,"+>$ARGV[0]"; foreach(@line){print FILE_W if(!/(\\\\)+/);}

=> Open and truncate your file on "write" mode, and write all data (but your \\ lines) into it.

I recommend this tool if you want to learn more about Perl :)

  • I should not recreate the file,with out recreating can i do that – deepuu May 20 '15 at 6:13

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