1

I would like to delete all the lines which have blank fields from text files. How can I change the following code to make the changes directly to the files?

awk '!/^\t|\t\t|\t$/' *.txt    

AD  125.9    MN 124.9
AC  38.9     VG 13.2
AV  34.6     BG 33.0
GL  126.2   
CY  34.9    
CY  44.9    

desired output

AD  125.9    MN 124.9
AC  38.9     VG 13.2
AV  34.6     BG 33.0
user1606106
  • 207
  • 1
  • 4
  • 9
  • are you mainly asking how to change the file in-place? check here: http://stackoverflow.com/questions/3979548/in-place-processing-with-grep – benroth Oct 01 '12 at 07:17
  • What is a 'blank field' in your context? Please provide an example of your input and of the desired output. –  Oct 01 '12 at 07:29

5 Answers5

7

alternative awk one-liner:

awk 'NF==4' yourFile
Kent
  • 189,393
  • 32
  • 233
  • 301
3

One way using GNU sed:

sed -n -s -i '/[^ \t]* [^ \t]* [^ \t]* [^ \t]/p' *.txt

Results:

AD  125.9    MN 124.9
AC  38.9     VG 13.2
AV  34.6     BG 33.0
Steve
  • 51,466
  • 13
  • 89
  • 103
  • Thank you very much for your answer. Your code worked correctly with the given example. But when it uses with my original files, it deletes all the lines from the files.Is there any other option for this? – user1606106 Oct 03 '12 at 13:24
  • @user1606106: This code suppresses printing with the `-n` flag. It considers files separately rather than as a continuous stream with the `-s` flag and edits files in place with the `-i` flag. When it finds a line with four non-empty fields or more, it will print them. So if your original files do not have four or more fields, they will be emptied. If you are able to upload some of your original files and some expected output for each to something like [dropbox](https://www.dropbox.com/) or [mediafire](http://www.mediafire.com/), perhaps I can help you. – Steve Oct 03 '12 at 22:57
0

You could use a for loop with your original awk command:

for f in *.txt
do
  cp $f /tmp/mytempfile
  awk '!/^\t|\t\t|\t$/' /tmp/mytempfile > $f
done
beny23
  • 34,390
  • 5
  • 82
  • 85
0

Using your input this might work (but it depends if there are spaces or tabs):

sed '/^.\{13\}\s/d' INPUTFILE

Or this:

awk 'substr($0,14,1) = " " { print }' INPUTFILE
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
0

assuming your file has a max of 4 fields:

awk '{for(i=1;i<5;i++){if($i=="")next;}print}' file_name

tested below:

> cat temp
AD      125.9    MN     124.9
AC      38.9     VG     13.2
AV      34.6     BG     33.0
GL      126.2
CY      34.9
CY      44.9
>awk '{for(i=1;i<5;i++){if($i=="")next;}print}' temp
AD      125.9    MN     124.9
AC      38.9     VG     13.2
AV      34.6     BG     33.0
>
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • Thank you very much for your answer. I don't need to print the output. I would like to make the changes directly to the original files. – user1606106 Oct 01 '12 at 08:46