Bash or Python for changing file spacing

I have a set of 10,000 files. In all of them, the second line looks like:

AAA 3.429 3.84 

So there is only one space between AAA and the other two columns (required). The rest of the lines on each file are completely different, corresponding to 10 columns of numbers.

Randomly, about 20%

BBB 3.429 3.84

So now there are two spaces between the first column and the second column .

This is a big error, so I need to fix it and change from 2 to 1 in the file where the error occurred.

The first method I thought of was to write A bash script, each file reads the 3 values ​​of the second line, then prints them with a space, and executes them for all files.

I want to know what oyu thinks about this method, if Can you suggest better, bashm python or other methods.

Thank you

Performing line-based text file changes in sed is usually the easiest.

sed -e '2s/ */ /g' infile.txt

Any run of multiple spaces will be replaced with one space. However, this may change more than you want.

sed -e '2s/^\ ([^ ]*\) /\1 /'infile.txt

An instance of two spaces after the first non-space text block should be replaced with only one space (though I haven’t tested it).

(Edit: insert 2 before s in each instance to bind the edit to the second line, specifically.)

I have a set of 10,000 files. In all of them, the second line looks like:

AAA 3.429 3.84

So AAA and others There is only one space between the two columns (required). The remaining rows on each file are completely different, corresponding to 10 columns of numbers.

Randomly, about 20% of the files, and due to some errors, one person gets

BBB 3.429 3.84

So now there are two spaces between the first column and the second column Grid.

This is a big error, so I need to fix it and change from 2 to 1 in the file where the error occurred.

The first method I thought of was Write a bash script, each file reads the 3 values ​​of the second line, and then prints them with a space, and executes them for all files.

I want to know what oyu thinks about this method, If you can make better suggestions, bashm python or other methods.

Thank you

Execute line-based text files in sed Changes are usually the easiest.

sed -e '2s/ */ /g' infile.txt

will replace multiple with one space Any run of spaces. However, this may change more than you want.

sed -e '2s/^\([^ ]*\) /\1 / 'infile.txt

There should be only one instance of replacing the two spaces after the first non-space text block with one space (though I haven’t tested it).

(Edit: in Insert 2 before s in each instance to bind editing to the second line, specifically.)

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5564 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.