Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make changes to only the first line of a file?

I would like to know which pattern can I use in sed to make changes in the first line of huge files (~2 GB). The preference for sed is only because I assume it must be faster than a Python or Perl script.

The files have the following structure:

field 1, field 2, ... field n
data

and, given the likelihood of having spaces in the identifier for every field, I need to replace every space by an underscore in this way:

**BEFORE** 
the first name,the second name,the first surname,a nickname, ...
data

**AFTER**
the_first_name,the_second_name,the_first_surname,a_nickname, ...
data

Any pointers to the right pattern to use, or another scripting solution would be great.

like image 674
Alex. S. Avatar asked Feb 14 '09 03:02

Alex. S.


2 Answers

To edit the first 10 lines

sed -i -e '1,10s/ /_/g'

In Perl, you can use the flip-flop operator in scalar context:

perl -i -pe 's/ /_/g if 1 .. 10'
like image 134
Leon Timmermans Avatar answered Oct 09 '22 00:10

Leon Timmermans


I don't think you want to use any solution that requires the data to be written to a new file.

If you're pretty sure that all you need is to change the spaces into underscores in the first line of the large text files, you only have to read the first line, swap the characters and write it back in place:

#!/usr/bin/env perl
use strict;

my $filename = shift;
open (FH, "+< $filename") || die "can't open $filename: $!";
my $line = <FH>;
$line =~ s/ /_/g;
seek FH, 0, 0; # go back to the start of the file
printf FH $line;
close FH;

To use it, just pass the full path of the file to update:

# fixheader "/path/to/myfile.txt"
like image 23
Renaud Bompuis Avatar answered Oct 09 '22 00:10

Renaud Bompuis