Computing.Net > Forums > Programming > delete line from the file if there is no thir

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

delete line from the file if there is no thir

Reply to Message Icon

Name: neeleshmrt
Date: May 22, 2009 at 04:04:47 Pacific
OS: LInux
Subcategory: Theory
Comment:

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 ,,,



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: May 22, 2009 at 04:18:00 Pacific
Reply:

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


0

Response Number 2
Name: neeleshmrt
Date: May 22, 2009 at 04:42:34 Pacific
Reply:

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.


0

Response Number 3
Name: ghostdog
Date: May 22, 2009 at 05:06:41 Pacific
Reply:

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.


0

Response Number 4
Name: neeleshmrt
Date: May 22, 2009 at 05:13:17 Pacific
Reply:

Thanks for you suggestion !


0

Response Number 5
Name: neeleshmrt
Date: May 22, 2009 at 05:52:36 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: reno
Date: May 22, 2009 at 06:16:39 Pacific
Reply:

cmd one-liner:
findstr/rc:"^.* .* .*" test.txt


0

Response Number 7
Name: ghostdog
Date: May 22, 2009 at 06:54:40 Pacific
Reply:

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

0

Response Number 8
Name: FishMonger
Date: May 22, 2009 at 07:14:14 Pacific
Reply:

Or, a perl 1 liner

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


0

Response Number 9
Name: reno
Date: May 22, 2009 at 08:02:33 Pacific
Reply:

oops, wrong thread.
just notice os is linux. =p


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: delete line from the file if there is no thir

Determine max. real number from external file www.computing.net/answers/programming/determine-max-real-number-from-external-file/20304.html

How to read the 3rd line from the T www.computing.net/answers/programming/how-to-read-the-3rd-line-from-the-t/16604.html

delete line from txt file if a colum is like! www.computing.net/answers/programming/delete-line-from-txt-file-if-a-colum-is-like/19419.html