delete line from the file if there is no thir

May 22, 2009 at 04:04:47
Specs: LInux
Please help me writing a spinet which can
delete the line from a file which does not have
thrird field

the file contains some like ,,,
xxx yyy zzz cccc yyyy
xxx yyy
xxx yyy nnn nnn nnn
xxx yyy nnn nnn nnn

like deleting the second line

Thanks in advance ,,,


See More: delete line from the file if there is no thir

Report •


#1
May 22, 2009 at 04:18:00
if you have Python
#!/usr/bin/env python
for line in open("file"):
    if len(line.split())>=3:
        print line.strip()

if you prefer awk

awk 'NF>=3' file


Report •

#2
May 22, 2009 at 04:42:34
Thanks for the fast response,but that 's not what i want to
do.Because of unfamiliarity i would not prefer to use
python..rather prefer using awk / sed

Problem here is i have a file having thousands of lines and i
want to delete all the lines that do not have third field.


Report •

#3
May 22, 2009 at 05:06:41
so try the awk one liner and see if its what you want. Being unfamiliar is not an excuse not to try out new things.

Report •

Related Solutions

#4
May 22, 2009 at 05:13:17
Thanks for you suggestion !


Report •

#5
May 22, 2009 at 05:52:36
I have the answer now ....

cat newio|\
while read LINE
do
echo $LINE
x=`echo $LINE | cut -d" " -f3`
y=${#x}
if [ $y -eq 0 ]
then
echo "do nthing"
else
echo $LINE >> temp1
fi
done


Report •

#6
May 22, 2009 at 06:16:39
cmd one-liner:
findstr/rc:"^.* .* .*" test.txt

Report •

#7
May 22, 2009 at 06:54:40
if you want to use some bash scripting instead of that awk one liner, then there is no need to use cat, which is useless in this case, and there is no need to use cut to get the 3rd field

while read a b c
do
 if [ ! -z "$c" ];then
    printf "$a $b $c\n"
 fi
done < file

for big files, try not to use bash's while read loop. its slower than using awk

Report •

#8
May 22, 2009 at 07:14:14
Or, a perl 1 liner

perl -lane 'print if @F > 3' newio


Report •

#9
May 22, 2009 at 08:02:33
oops, wrong thread.
just notice os is linux. =p

Report •


Ask Question