Delete lines from file

I am doing some text processing on a unix system. I have command line access on this computer and it has Python, Perl and the default word processing programs, awk, etc.

I have a text file that looks like this:

2029754527851451717 
2029754527851451717 
2029754527851451717 
2029754527851451717 
2029754527851451717 
2029754527851451717 1232453488239 Tue Mar  3 10:47:44 2009
2029754527851451717 1232453488302 Tue Mar  3 10:47:44 2009
2029754527851451717 1232453488365 Tue Mar  3 10:47:44 2009
2895635937120524206 
2895635937120524206 
2895635937120524206 
2895635937120524206 
2895635937120524206 
2895635937120524206 
5622983575622325494 1232453323986 Thu Feb 12 15:57:49 2009

These are basically 3 lines: ID ID Date

I want to delete all rows that do not have 2 identifiers and dates. So, the final results will be as follows:

2029754527851451717 1232453488239 Tue Mar  3 10:47:44 2009
2029754527851451717 1232453488302 Tue Mar  3 10:47:44 2009
2029754527851451717 1232453488365 Tue Mar  3 10:47:44 2009
5622983575622325494 1232453323986 Thu Feb 12 15:57:49 2009

How would you decide to do this? In general, a text file is about 30,000 lines long.

Greetings

Eef

+3
source share
9 answers

C awk:

awk 'NF > 2' input_file > output_file
+14
source
grep ':' filename
+8
with open(source_filename) as src:
    with open(dest_filename, 'w') as dst:
        for line in src:
            if len(line.split()) > 1:
                dst.write(line)
+5

Perl:

perl -ne 'print if /^([0-9]+\s+){2}.+$/' $filename
+4

, :

perl -lane 'if (scalar(@F) == 3) { print @F;}' file >> file.out
+3

Python:

file = open(filename, 'r')
lines = file.readlines()
file.close()

p = re.compile('^\d*$')

for line in lines:
    if not p.search(line): print line,
+2
awk "NF>1" < filename
+1
source
perl -i -lane 'print if($F[1])' file
+1
source
sed '/^[0-9]$/d'  filename

(you may have to change the template if the incorrect lines have spaces). You can also use grep -v, which will skip the matching pattern.

-1
source

Source: https://habr.com/ru/post/1707192/


All Articles