How can I change ^L code in many files in Ubuntu?find: unknown predicateDifferent behavior when launched from terminal instead of Unity launcherRecursively apply the msgfmt command to all .po files in directory with find -execHow do I replace multiple lines with single word in file(inplace replace)?Is there a bash/terminal command that will clear content prior to the last command?Print sub-folder name and content of result.txt to .csvPutting a newline whenever it match third spaceHow can I change lower to upper using (three) parameters?bash filename autocomplete - does it have to use so much screen space?Regex for /etc/passwd Content
Problem with GFCI at start of circuit with both lights and two receptacles
When does The Truman Show take place?
Would getting a natural 20 with a penalty still count as a critical hit?
What's the relationship betweeen MS-DOS and XENIX?
What was the intention with the Commodore 128?
What allows us to use imaginary numbers?
Build a mob of suspiciously happy lenny faces ( ͡° ͜ʖ ͡°)
What if a restaurant suddenly cannot accept credit cards, and the customer has no cash?
Why won't the Republicans use a superdelegate system like the DNC in their nomination process?
Unconventional examples of mathematical modelling
Change the default Bookmarks Folder In Firefox
What is the question mark?
Does Medium Armor's Max dex also put a cap on the negative side?
Why does "auf der Strecke bleiben" mean "to fall by the wayside"?
What does 〇〇〇〇 mean when combined with おじさん?
Is Fourier series a sampled version of Fourier transform?
Expressing a chain of boolean ORs using ILP
What's the point of writing that I know will never be used or read?
Is nullptr falsy?
Has there ever been a truly bilingual country prior to the contemporary period?
Will Force.com stop working on salesforce Lightning?
How would armour (and combat) change if the fighter didn't need to actually wear it?
What are the advantages of this gold finger shape?
What should I do if actually I found a serious flaw in someone's PhD thesis and an article derived from that PhD thesis?
How can I change ^L code in many files in Ubuntu?
find: unknown predicateDifferent behavior when launched from terminal instead of Unity launcherRecursively apply the msgfmt command to all .po files in directory with find -execHow do I replace multiple lines with single word in file(inplace replace)?Is there a bash/terminal command that will clear content prior to the last command?Print sub-folder name and content of result.txt to .csvPutting a newline whenever it match third spaceHow can I change lower to upper using (three) parameters?bash filename autocomplete - does it have to use so much screen space?Regex for /etc/passwd Content
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a lot of XML files, over 50000 of them.
In some XML files, some files are written like this
<filename>abc.JPEG<^Lilename>
^L
is just one character, but I can't find what ^L
means with Google.
When I use cat
to print the content of a file, it shows like the following
<filename>abc.JPEG<
ilename>
Anyway, I want to change <filename>abc.JPEG<^Lilename>
to <filename>abc.JPEG</filename>
I already found some command to change a word in many files, such as
find . -exec perl -pi -e 's/[find_word]/[change_word]/g' ;
But that command doesn't work in my case, because it cannot recognize the search word when I just type ^L
.
How can I change <filename>abc.JPEG<^Lilename>
to <filename>abc.JPEG</filename>
in many files?
bash perl
add a comment |
I have a lot of XML files, over 50000 of them.
In some XML files, some files are written like this
<filename>abc.JPEG<^Lilename>
^L
is just one character, but I can't find what ^L
means with Google.
When I use cat
to print the content of a file, it shows like the following
<filename>abc.JPEG<
ilename>
Anyway, I want to change <filename>abc.JPEG<^Lilename>
to <filename>abc.JPEG</filename>
I already found some command to change a word in many files, such as
find . -exec perl -pi -e 's/[find_word]/[change_word]/g' ;
But that command doesn't work in my case, because it cannot recognize the search word when I just type ^L
.
How can I change <filename>abc.JPEG<^Lilename>
to <filename>abc.JPEG</filename>
in many files?
bash perl
6
Apparently someone used<filename>
instead of</filename>
in a context wheref
would be interpreted as the form feed character. You should probably track down the source of these files and point out the problem with their generating tool to the developer. For fixing the files, the accepted answer is just fine.
– Hans-Martin Mosner
Aug 5 at 10:41
add a comment |
I have a lot of XML files, over 50000 of them.
In some XML files, some files are written like this
<filename>abc.JPEG<^Lilename>
^L
is just one character, but I can't find what ^L
means with Google.
When I use cat
to print the content of a file, it shows like the following
<filename>abc.JPEG<
ilename>
Anyway, I want to change <filename>abc.JPEG<^Lilename>
to <filename>abc.JPEG</filename>
I already found some command to change a word in many files, such as
find . -exec perl -pi -e 's/[find_word]/[change_word]/g' ;
But that command doesn't work in my case, because it cannot recognize the search word when I just type ^L
.
How can I change <filename>abc.JPEG<^Lilename>
to <filename>abc.JPEG</filename>
in many files?
bash perl
I have a lot of XML files, over 50000 of them.
In some XML files, some files are written like this
<filename>abc.JPEG<^Lilename>
^L
is just one character, but I can't find what ^L
means with Google.
When I use cat
to print the content of a file, it shows like the following
<filename>abc.JPEG<
ilename>
Anyway, I want to change <filename>abc.JPEG<^Lilename>
to <filename>abc.JPEG</filename>
I already found some command to change a word in many files, such as
find . -exec perl -pi -e 's/[find_word]/[change_word]/g' ;
But that command doesn't work in my case, because it cannot recognize the search word when I just type ^L
.
How can I change <filename>abc.JPEG<^Lilename>
to <filename>abc.JPEG</filename>
in many files?
bash perl
bash perl
edited Aug 5 at 11:36
Dan
7,4005 gold badges47 silver badges74 bronze badges
7,4005 gold badges47 silver badges74 bronze badges
asked Aug 4 at 12:45
YangYang
504 bronze badges
504 bronze badges
6
Apparently someone used<filename>
instead of</filename>
in a context wheref
would be interpreted as the form feed character. You should probably track down the source of these files and point out the problem with their generating tool to the developer. For fixing the files, the accepted answer is just fine.
– Hans-Martin Mosner
Aug 5 at 10:41
add a comment |
6
Apparently someone used<filename>
instead of</filename>
in a context wheref
would be interpreted as the form feed character. You should probably track down the source of these files and point out the problem with their generating tool to the developer. For fixing the files, the accepted answer is just fine.
– Hans-Martin Mosner
Aug 5 at 10:41
6
6
Apparently someone used
<filename>
instead of </filename>
in a context where f
would be interpreted as the form feed character. You should probably track down the source of these files and point out the problem with their generating tool to the developer. For fixing the files, the accepted answer is just fine.– Hans-Martin Mosner
Aug 5 at 10:41
Apparently someone used
<filename>
instead of </filename>
in a context where f
would be interpreted as the form feed character. You should probably track down the source of these files and point out the problem with their generating tool to the developer. For fixing the files, the accepted answer is just fine.– Hans-Martin Mosner
Aug 5 at 10:41
add a comment |
3 Answers
3
active
oldest
votes
Control-L (represented as ^L
) is the "form feed" character. In ASCII, it has decimal value 12 (L
is the 12th letter of the alphabet) or hex value 0c:
$ printf 'foox0cbarn' | cat -et
foo^Lbar$
$ printf 'foox0cbarn'
foo
bar
You can replace it using tools like sed by specifying the hexadecimal escape code:
$ printf 'foox0cbarn' | sed 's/x0c//'
foobar
Alternatively, compose ^L
directly using the keyboard sequence CTRL+V CTRL+L
sed 's/CTRL+VCTRL+L//'
For your specific replacement, given
$ printf '<x0cilenamen'
<
ilename
then
$ printf '<x0cilenamen' | sed 's/<x0c/</f/g'
</filename
(the g
modifier is added in case there is more than one instance per line).
In my case, "$ printf '<x0cilenamen' | sed 's/<x0c/<\f/g '" isn't working. But, according to your answer, "$ find . -exec perl -pi -e 's/<x0cilename>/</filename>/g' ;" works well. Thanks for your answer:)
– Yang
Aug 4 at 15:36
@Yang sorry I just realised that I confused forward slash and backslash in my answer (corrected now) - still not sure why that would have prevented the sed version from working though
– steeldriver
Aug 4 at 16:05
A very good answer! It would be even better if it included say afind
that looped over those 50000 XML files and automatically processed each one (and made a backup too).
– Kingsley
Aug 5 at 4:00
add a comment |
As Hans-Martin Mosner points out in the comments, it seems that someone used backslashes instead of forward slashes when generating the XML (or possibly ran the whole <filename>
section through a Unix-to-Windows converter which was overzealous about slashes). f
is a rarely-used escape sequence for a form-feed character, aka U+0C or ^L. So some later step of the pipeline then replaced the f
with literal U+0C characters.
Fortunately, U+0C is an extremely rare character that's unlikely to be found intentionally in any sort of XML. And since only f
would produce this, as opposed to (say) g
or k
, a universal find-and-replace should fix not only </filename>
but also </folder>
, </file>
, or anything else that got mangled.
That's what steeldriver's sed-script does; I'd just make it very slightly more general:
sed 's|x0c|/f|g'
This means "(s)wap all instances of x0c
(that is, U+0C) to /f
, (g)lobally".
add a comment |
f
is the form feed character in Perl. It looks as though these malformed files were created by someone new to both Perl and XML.
Here's a much Perlier fix -- which also meets the OP's goals of automating update of all of the files, unlike the accepted answer with sed, which will only work on one file at a time as it isn't paired with find
.
f
can simply be employed itself instead of the hexadecimal code x0c
.
find . -type f -exec perl -pi.bkp -e 's [ f ilename ][ /f ilename ]gx' ;
Here I've added -type f
to tel find
to only return plain files - otherwise find
will return .
in the list, and trigger a warning when you try to edit it, though everything else will still work.
I've also made the regex easier to see by using the x
flag which ignores real whitespace, allowing you to space out the elements of your regex. If you don't like this, here it is without:
find . -type f -exec perl -pi.bkp -e 's[filename][/filename]g' ;
And in the likely case that all the form feed characters are spurious and all should be replaced by /f
, then you can slim the one-liner down even further:
find . -type f -exec perl -pi.bkp -e 's[f][/f]g' ;
You don't need to use forward slashes to surround your regex substitution command's elements (s///
) in Perl. You can use any symbol. If you choose to use any kind of paired bracket-like symbol, however, you have to use both of them: s[old][new]
for instance.
Since I'm not using slashes, I don't have to escape any slashes.
As for -i.bkp
: perl -pi -e
lets you edit in-place -- but if you want extra insurance in case you got your find-and-replace Perl program wrong, you can put in a file extension so that it will make a copy of the original files for you. Here, I've used .bkp
.
In the most recent versions of Perl, in-place editing has been updated to be more resilient in case your system suffers a serious problem like power loss or running out of disk space, too. Here's Perl author brian d foy on improved in-place editing in recent Perls.
You should consider using Perl for these kinds of tasks, because it is an extremely powerful yet under-rated general-purpose programming language, one of whose original design goals was to replace sed
and awk
with something much better.
Perl 5's regex matching capabilities and improved regex syntax far exceed those of sed
, awk
, and indeed every other programming language apart from Perl 6, making Perl the most sensible choice for both simple and advanced regex manipulations.
To clarify: sed
will work OK with find
too and you can also use sed -i.bkp
to make a backup of each file edited, but as far as I know it doesn't feature the extra resilience in Perl 5.28 and above. It also uses the clunkier and far less powerful traditional UNIX ® regex syntax.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1163335%2fhow-can-i-change-l-code-in-many-files-in-ubuntu%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Control-L (represented as ^L
) is the "form feed" character. In ASCII, it has decimal value 12 (L
is the 12th letter of the alphabet) or hex value 0c:
$ printf 'foox0cbarn' | cat -et
foo^Lbar$
$ printf 'foox0cbarn'
foo
bar
You can replace it using tools like sed by specifying the hexadecimal escape code:
$ printf 'foox0cbarn' | sed 's/x0c//'
foobar
Alternatively, compose ^L
directly using the keyboard sequence CTRL+V CTRL+L
sed 's/CTRL+VCTRL+L//'
For your specific replacement, given
$ printf '<x0cilenamen'
<
ilename
then
$ printf '<x0cilenamen' | sed 's/<x0c/</f/g'
</filename
(the g
modifier is added in case there is more than one instance per line).
In my case, "$ printf '<x0cilenamen' | sed 's/<x0c/<\f/g '" isn't working. But, according to your answer, "$ find . -exec perl -pi -e 's/<x0cilename>/</filename>/g' ;" works well. Thanks for your answer:)
– Yang
Aug 4 at 15:36
@Yang sorry I just realised that I confused forward slash and backslash in my answer (corrected now) - still not sure why that would have prevented the sed version from working though
– steeldriver
Aug 4 at 16:05
A very good answer! It would be even better if it included say afind
that looped over those 50000 XML files and automatically processed each one (and made a backup too).
– Kingsley
Aug 5 at 4:00
add a comment |
Control-L (represented as ^L
) is the "form feed" character. In ASCII, it has decimal value 12 (L
is the 12th letter of the alphabet) or hex value 0c:
$ printf 'foox0cbarn' | cat -et
foo^Lbar$
$ printf 'foox0cbarn'
foo
bar
You can replace it using tools like sed by specifying the hexadecimal escape code:
$ printf 'foox0cbarn' | sed 's/x0c//'
foobar
Alternatively, compose ^L
directly using the keyboard sequence CTRL+V CTRL+L
sed 's/CTRL+VCTRL+L//'
For your specific replacement, given
$ printf '<x0cilenamen'
<
ilename
then
$ printf '<x0cilenamen' | sed 's/<x0c/</f/g'
</filename
(the g
modifier is added in case there is more than one instance per line).
In my case, "$ printf '<x0cilenamen' | sed 's/<x0c/<\f/g '" isn't working. But, according to your answer, "$ find . -exec perl -pi -e 's/<x0cilename>/</filename>/g' ;" works well. Thanks for your answer:)
– Yang
Aug 4 at 15:36
@Yang sorry I just realised that I confused forward slash and backslash in my answer (corrected now) - still not sure why that would have prevented the sed version from working though
– steeldriver
Aug 4 at 16:05
A very good answer! It would be even better if it included say afind
that looped over those 50000 XML files and automatically processed each one (and made a backup too).
– Kingsley
Aug 5 at 4:00
add a comment |
Control-L (represented as ^L
) is the "form feed" character. In ASCII, it has decimal value 12 (L
is the 12th letter of the alphabet) or hex value 0c:
$ printf 'foox0cbarn' | cat -et
foo^Lbar$
$ printf 'foox0cbarn'
foo
bar
You can replace it using tools like sed by specifying the hexadecimal escape code:
$ printf 'foox0cbarn' | sed 's/x0c//'
foobar
Alternatively, compose ^L
directly using the keyboard sequence CTRL+V CTRL+L
sed 's/CTRL+VCTRL+L//'
For your specific replacement, given
$ printf '<x0cilenamen'
<
ilename
then
$ printf '<x0cilenamen' | sed 's/<x0c/</f/g'
</filename
(the g
modifier is added in case there is more than one instance per line).
Control-L (represented as ^L
) is the "form feed" character. In ASCII, it has decimal value 12 (L
is the 12th letter of the alphabet) or hex value 0c:
$ printf 'foox0cbarn' | cat -et
foo^Lbar$
$ printf 'foox0cbarn'
foo
bar
You can replace it using tools like sed by specifying the hexadecimal escape code:
$ printf 'foox0cbarn' | sed 's/x0c//'
foobar
Alternatively, compose ^L
directly using the keyboard sequence CTRL+V CTRL+L
sed 's/CTRL+VCTRL+L//'
For your specific replacement, given
$ printf '<x0cilenamen'
<
ilename
then
$ printf '<x0cilenamen' | sed 's/<x0c/</f/g'
</filename
(the g
modifier is added in case there is more than one instance per line).
edited Aug 4 at 15:48
answered Aug 4 at 13:09
steeldriversteeldriver
77.7k12 gold badges129 silver badges208 bronze badges
77.7k12 gold badges129 silver badges208 bronze badges
In my case, "$ printf '<x0cilenamen' | sed 's/<x0c/<\f/g '" isn't working. But, according to your answer, "$ find . -exec perl -pi -e 's/<x0cilename>/</filename>/g' ;" works well. Thanks for your answer:)
– Yang
Aug 4 at 15:36
@Yang sorry I just realised that I confused forward slash and backslash in my answer (corrected now) - still not sure why that would have prevented the sed version from working though
– steeldriver
Aug 4 at 16:05
A very good answer! It would be even better if it included say afind
that looped over those 50000 XML files and automatically processed each one (and made a backup too).
– Kingsley
Aug 5 at 4:00
add a comment |
In my case, "$ printf '<x0cilenamen' | sed 's/<x0c/<\f/g '" isn't working. But, according to your answer, "$ find . -exec perl -pi -e 's/<x0cilename>/</filename>/g' ;" works well. Thanks for your answer:)
– Yang
Aug 4 at 15:36
@Yang sorry I just realised that I confused forward slash and backslash in my answer (corrected now) - still not sure why that would have prevented the sed version from working though
– steeldriver
Aug 4 at 16:05
A very good answer! It would be even better if it included say afind
that looped over those 50000 XML files and automatically processed each one (and made a backup too).
– Kingsley
Aug 5 at 4:00
In my case, "$ printf '<x0cilenamen' | sed 's/<x0c/<\f/g '" isn't working. But, according to your answer, "$ find . -exec perl -pi -e 's/<x0cilename>/</filename>/g' ;" works well. Thanks for your answer:)
– Yang
Aug 4 at 15:36
In my case, "$ printf '<x0cilenamen' | sed 's/<x0c/<\f/g '" isn't working. But, according to your answer, "$ find . -exec perl -pi -e 's/<x0cilename>/</filename>/g' ;" works well. Thanks for your answer:)
– Yang
Aug 4 at 15:36
@Yang sorry I just realised that I confused forward slash and backslash in my answer (corrected now) - still not sure why that would have prevented the sed version from working though
– steeldriver
Aug 4 at 16:05
@Yang sorry I just realised that I confused forward slash and backslash in my answer (corrected now) - still not sure why that would have prevented the sed version from working though
– steeldriver
Aug 4 at 16:05
A very good answer! It would be even better if it included say a
find
that looped over those 50000 XML files and automatically processed each one (and made a backup too).– Kingsley
Aug 5 at 4:00
A very good answer! It would be even better if it included say a
find
that looped over those 50000 XML files and automatically processed each one (and made a backup too).– Kingsley
Aug 5 at 4:00
add a comment |
As Hans-Martin Mosner points out in the comments, it seems that someone used backslashes instead of forward slashes when generating the XML (or possibly ran the whole <filename>
section through a Unix-to-Windows converter which was overzealous about slashes). f
is a rarely-used escape sequence for a form-feed character, aka U+0C or ^L. So some later step of the pipeline then replaced the f
with literal U+0C characters.
Fortunately, U+0C is an extremely rare character that's unlikely to be found intentionally in any sort of XML. And since only f
would produce this, as opposed to (say) g
or k
, a universal find-and-replace should fix not only </filename>
but also </folder>
, </file>
, or anything else that got mangled.
That's what steeldriver's sed-script does; I'd just make it very slightly more general:
sed 's|x0c|/f|g'
This means "(s)wap all instances of x0c
(that is, U+0C) to /f
, (g)lobally".
add a comment |
As Hans-Martin Mosner points out in the comments, it seems that someone used backslashes instead of forward slashes when generating the XML (or possibly ran the whole <filename>
section through a Unix-to-Windows converter which was overzealous about slashes). f
is a rarely-used escape sequence for a form-feed character, aka U+0C or ^L. So some later step of the pipeline then replaced the f
with literal U+0C characters.
Fortunately, U+0C is an extremely rare character that's unlikely to be found intentionally in any sort of XML. And since only f
would produce this, as opposed to (say) g
or k
, a universal find-and-replace should fix not only </filename>
but also </folder>
, </file>
, or anything else that got mangled.
That's what steeldriver's sed-script does; I'd just make it very slightly more general:
sed 's|x0c|/f|g'
This means "(s)wap all instances of x0c
(that is, U+0C) to /f
, (g)lobally".
add a comment |
As Hans-Martin Mosner points out in the comments, it seems that someone used backslashes instead of forward slashes when generating the XML (or possibly ran the whole <filename>
section through a Unix-to-Windows converter which was overzealous about slashes). f
is a rarely-used escape sequence for a form-feed character, aka U+0C or ^L. So some later step of the pipeline then replaced the f
with literal U+0C characters.
Fortunately, U+0C is an extremely rare character that's unlikely to be found intentionally in any sort of XML. And since only f
would produce this, as opposed to (say) g
or k
, a universal find-and-replace should fix not only </filename>
but also </folder>
, </file>
, or anything else that got mangled.
That's what steeldriver's sed-script does; I'd just make it very slightly more general:
sed 's|x0c|/f|g'
This means "(s)wap all instances of x0c
(that is, U+0C) to /f
, (g)lobally".
As Hans-Martin Mosner points out in the comments, it seems that someone used backslashes instead of forward slashes when generating the XML (or possibly ran the whole <filename>
section through a Unix-to-Windows converter which was overzealous about slashes). f
is a rarely-used escape sequence for a form-feed character, aka U+0C or ^L. So some later step of the pipeline then replaced the f
with literal U+0C characters.
Fortunately, U+0C is an extremely rare character that's unlikely to be found intentionally in any sort of XML. And since only f
would produce this, as opposed to (say) g
or k
, a universal find-and-replace should fix not only </filename>
but also </folder>
, </file>
, or anything else that got mangled.
That's what steeldriver's sed-script does; I'd just make it very slightly more general:
sed 's|x0c|/f|g'
This means "(s)wap all instances of x0c
(that is, U+0C) to /f
, (g)lobally".
answered Aug 5 at 16:05
DraconisDraconis
2311 silver badge6 bronze badges
2311 silver badge6 bronze badges
add a comment |
add a comment |
f
is the form feed character in Perl. It looks as though these malformed files were created by someone new to both Perl and XML.
Here's a much Perlier fix -- which also meets the OP's goals of automating update of all of the files, unlike the accepted answer with sed, which will only work on one file at a time as it isn't paired with find
.
f
can simply be employed itself instead of the hexadecimal code x0c
.
find . -type f -exec perl -pi.bkp -e 's [ f ilename ][ /f ilename ]gx' ;
Here I've added -type f
to tel find
to only return plain files - otherwise find
will return .
in the list, and trigger a warning when you try to edit it, though everything else will still work.
I've also made the regex easier to see by using the x
flag which ignores real whitespace, allowing you to space out the elements of your regex. If you don't like this, here it is without:
find . -type f -exec perl -pi.bkp -e 's[filename][/filename]g' ;
And in the likely case that all the form feed characters are spurious and all should be replaced by /f
, then you can slim the one-liner down even further:
find . -type f -exec perl -pi.bkp -e 's[f][/f]g' ;
You don't need to use forward slashes to surround your regex substitution command's elements (s///
) in Perl. You can use any symbol. If you choose to use any kind of paired bracket-like symbol, however, you have to use both of them: s[old][new]
for instance.
Since I'm not using slashes, I don't have to escape any slashes.
As for -i.bkp
: perl -pi -e
lets you edit in-place -- but if you want extra insurance in case you got your find-and-replace Perl program wrong, you can put in a file extension so that it will make a copy of the original files for you. Here, I've used .bkp
.
In the most recent versions of Perl, in-place editing has been updated to be more resilient in case your system suffers a serious problem like power loss or running out of disk space, too. Here's Perl author brian d foy on improved in-place editing in recent Perls.
You should consider using Perl for these kinds of tasks, because it is an extremely powerful yet under-rated general-purpose programming language, one of whose original design goals was to replace sed
and awk
with something much better.
Perl 5's regex matching capabilities and improved regex syntax far exceed those of sed
, awk
, and indeed every other programming language apart from Perl 6, making Perl the most sensible choice for both simple and advanced regex manipulations.
To clarify: sed
will work OK with find
too and you can also use sed -i.bkp
to make a backup of each file edited, but as far as I know it doesn't feature the extra resilience in Perl 5.28 and above. It also uses the clunkier and far less powerful traditional UNIX ® regex syntax.
add a comment |
f
is the form feed character in Perl. It looks as though these malformed files were created by someone new to both Perl and XML.
Here's a much Perlier fix -- which also meets the OP's goals of automating update of all of the files, unlike the accepted answer with sed, which will only work on one file at a time as it isn't paired with find
.
f
can simply be employed itself instead of the hexadecimal code x0c
.
find . -type f -exec perl -pi.bkp -e 's [ f ilename ][ /f ilename ]gx' ;
Here I've added -type f
to tel find
to only return plain files - otherwise find
will return .
in the list, and trigger a warning when you try to edit it, though everything else will still work.
I've also made the regex easier to see by using the x
flag which ignores real whitespace, allowing you to space out the elements of your regex. If you don't like this, here it is without:
find . -type f -exec perl -pi.bkp -e 's[filename][/filename]g' ;
And in the likely case that all the form feed characters are spurious and all should be replaced by /f
, then you can slim the one-liner down even further:
find . -type f -exec perl -pi.bkp -e 's[f][/f]g' ;
You don't need to use forward slashes to surround your regex substitution command's elements (s///
) in Perl. You can use any symbol. If you choose to use any kind of paired bracket-like symbol, however, you have to use both of them: s[old][new]
for instance.
Since I'm not using slashes, I don't have to escape any slashes.
As for -i.bkp
: perl -pi -e
lets you edit in-place -- but if you want extra insurance in case you got your find-and-replace Perl program wrong, you can put in a file extension so that it will make a copy of the original files for you. Here, I've used .bkp
.
In the most recent versions of Perl, in-place editing has been updated to be more resilient in case your system suffers a serious problem like power loss or running out of disk space, too. Here's Perl author brian d foy on improved in-place editing in recent Perls.
You should consider using Perl for these kinds of tasks, because it is an extremely powerful yet under-rated general-purpose programming language, one of whose original design goals was to replace sed
and awk
with something much better.
Perl 5's regex matching capabilities and improved regex syntax far exceed those of sed
, awk
, and indeed every other programming language apart from Perl 6, making Perl the most sensible choice for both simple and advanced regex manipulations.
To clarify: sed
will work OK with find
too and you can also use sed -i.bkp
to make a backup of each file edited, but as far as I know it doesn't feature the extra resilience in Perl 5.28 and above. It also uses the clunkier and far less powerful traditional UNIX ® regex syntax.
add a comment |
f
is the form feed character in Perl. It looks as though these malformed files were created by someone new to both Perl and XML.
Here's a much Perlier fix -- which also meets the OP's goals of automating update of all of the files, unlike the accepted answer with sed, which will only work on one file at a time as it isn't paired with find
.
f
can simply be employed itself instead of the hexadecimal code x0c
.
find . -type f -exec perl -pi.bkp -e 's [ f ilename ][ /f ilename ]gx' ;
Here I've added -type f
to tel find
to only return plain files - otherwise find
will return .
in the list, and trigger a warning when you try to edit it, though everything else will still work.
I've also made the regex easier to see by using the x
flag which ignores real whitespace, allowing you to space out the elements of your regex. If you don't like this, here it is without:
find . -type f -exec perl -pi.bkp -e 's[filename][/filename]g' ;
And in the likely case that all the form feed characters are spurious and all should be replaced by /f
, then you can slim the one-liner down even further:
find . -type f -exec perl -pi.bkp -e 's[f][/f]g' ;
You don't need to use forward slashes to surround your regex substitution command's elements (s///
) in Perl. You can use any symbol. If you choose to use any kind of paired bracket-like symbol, however, you have to use both of them: s[old][new]
for instance.
Since I'm not using slashes, I don't have to escape any slashes.
As for -i.bkp
: perl -pi -e
lets you edit in-place -- but if you want extra insurance in case you got your find-and-replace Perl program wrong, you can put in a file extension so that it will make a copy of the original files for you. Here, I've used .bkp
.
In the most recent versions of Perl, in-place editing has been updated to be more resilient in case your system suffers a serious problem like power loss or running out of disk space, too. Here's Perl author brian d foy on improved in-place editing in recent Perls.
You should consider using Perl for these kinds of tasks, because it is an extremely powerful yet under-rated general-purpose programming language, one of whose original design goals was to replace sed
and awk
with something much better.
Perl 5's regex matching capabilities and improved regex syntax far exceed those of sed
, awk
, and indeed every other programming language apart from Perl 6, making Perl the most sensible choice for both simple and advanced regex manipulations.
To clarify: sed
will work OK with find
too and you can also use sed -i.bkp
to make a backup of each file edited, but as far as I know it doesn't feature the extra resilience in Perl 5.28 and above. It also uses the clunkier and far less powerful traditional UNIX ® regex syntax.
f
is the form feed character in Perl. It looks as though these malformed files were created by someone new to both Perl and XML.
Here's a much Perlier fix -- which also meets the OP's goals of automating update of all of the files, unlike the accepted answer with sed, which will only work on one file at a time as it isn't paired with find
.
f
can simply be employed itself instead of the hexadecimal code x0c
.
find . -type f -exec perl -pi.bkp -e 's [ f ilename ][ /f ilename ]gx' ;
Here I've added -type f
to tel find
to only return plain files - otherwise find
will return .
in the list, and trigger a warning when you try to edit it, though everything else will still work.
I've also made the regex easier to see by using the x
flag which ignores real whitespace, allowing you to space out the elements of your regex. If you don't like this, here it is without:
find . -type f -exec perl -pi.bkp -e 's[filename][/filename]g' ;
And in the likely case that all the form feed characters are spurious and all should be replaced by /f
, then you can slim the one-liner down even further:
find . -type f -exec perl -pi.bkp -e 's[f][/f]g' ;
You don't need to use forward slashes to surround your regex substitution command's elements (s///
) in Perl. You can use any symbol. If you choose to use any kind of paired bracket-like symbol, however, you have to use both of them: s[old][new]
for instance.
Since I'm not using slashes, I don't have to escape any slashes.
As for -i.bkp
: perl -pi -e
lets you edit in-place -- but if you want extra insurance in case you got your find-and-replace Perl program wrong, you can put in a file extension so that it will make a copy of the original files for you. Here, I've used .bkp
.
In the most recent versions of Perl, in-place editing has been updated to be more resilient in case your system suffers a serious problem like power loss or running out of disk space, too. Here's Perl author brian d foy on improved in-place editing in recent Perls.
You should consider using Perl for these kinds of tasks, because it is an extremely powerful yet under-rated general-purpose programming language, one of whose original design goals was to replace sed
and awk
with something much better.
Perl 5's regex matching capabilities and improved regex syntax far exceed those of sed
, awk
, and indeed every other programming language apart from Perl 6, making Perl the most sensible choice for both simple and advanced regex manipulations.
To clarify: sed
will work OK with find
too and you can also use sed -i.bkp
to make a backup of each file edited, but as far as I know it doesn't feature the extra resilience in Perl 5.28 and above. It also uses the clunkier and far less powerful traditional UNIX ® regex syntax.
edited Aug 10 at 18:01
answered Aug 5 at 21:05
Medlock PerlmanMedlock Perlman
213 bronze badges
213 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1163335%2fhow-can-i-change-l-code-in-many-files-in-ubuntu%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
Apparently someone used
<filename>
instead of</filename>
in a context wheref
would be interpreted as the form feed character. You should probably track down the source of these files and point out the problem with their generating tool to the developer. For fixing the files, the accepted answer is just fine.– Hans-Martin Mosner
Aug 5 at 10:41