Reputation: 207
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
Upvotes: 1
Views: 3878
Reputation: 54402
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
Upvotes: 3
Reputation: 67231
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
>
Upvotes: 0
Reputation: 51613
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
Upvotes: 0
Reputation: 35018
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
Upvotes: 0