Forum: Cfengine Help
Subject: Editing only the first line in a file
Author: sauer
Link to topic: https://cfengine.com/forum/read.php?3,21478,21478#msg-21478
Let me know if I'm missing something. I want to make sure that the first line
in a file is a certain value - specifically, if the first line is like
#!.*perl, I want to replace it with #!/path/to/perl -U. I know that this will
only be the first line in a file, but some of the files are pretty long. I'd
rather not waste a bunch of time comparing every line with a regex (I have a
lot of files to check, so performance matters). Right now, I have
body select_region perl_interpreter_line {
select_start => "#!/\S*perl.*";
select_end => "(?!#!).*";
include_start_delimiter => "true";
include_end_delimiter => "false";
}
But that still seems like an awful lot of work (both on my part with typing,
and on Cfengine's part with extra regex comparisons) when I know that I really
just want to work on the first line. It also gripes a lot in files which don't
have a perl bangpath, which is more of a nuiscance than anything.
I noticed there's a comment in the documentation of both location and
match_value for "select_line_matching" which says
"This attribute is mutually exclusive of ‘select_line_number’."
but there's no other reference of select_line_number in location - just
match_value. So, I thought that perhaps I'd try
body select_region first_line {
select_line_number => "1";
}
Unfortunately, that segfaults - which I assume means it doesn't work. :) Are
there any plans to add a select_line_number (or, even better, a sed-like
start-line and end-line) capability to the select_region body? I noticed that
specifying an end pattern without a start pattern also segfaults, which I had
hoped might select from the beginning of the file to the delimiter. Any better
suggestions?
Thanks,
Danny
_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine