Skip to main content
added 43 characters in body
Source Link

For a migration process, I need to do some replacement in my bash script.

So in my .txt files, I have these references for example:

[[File:My Image.png|400px|thumb|center|My Image]]
[[File:My Image.png|400px|thumb|center]]
[[File:My Image.png|400px|thumb]]
[[File:My Image.png|400px]]
[[File:My Image.png]]

What I need to do is to replace all these occurrences with this line (only - so no more size, description, or other informations):

[[File:My Image.png]]

So, I tried to do a is to build a PCRE regex to extract all images names with:

/File:(.*\..{3})/g

I've built this final command to find all occurrences in my .txt files and extract image name with my regex:

find . -name "*.txt" | xargs perl -i -p -e 's/File:(.*\..{3})/$1/g'

But, I've encountered some problems as:

  • An error:

xargs: unterminated quote

  • And finally I don't know how to use extracted image name to replace all occurrences (complete lines)

PS: I'm on MacOS system and use bash v4

For a migration process, I need to do some replacement in my bash script.

So in my .txt files, I have these references for example:

[[File:My Image.png|400px|thumb|center|My Image]]
[[File:My Image.png|400px|thumb|center]]
[[File:My Image.png|400px|thumb]]
[[File:My Image.png|400px]]
[[File:My Image.png]]

What I need to do is to replace all these occurrences with this line (only - so no more size, description, or other informations):

[[File:My Image.png]]

So, I tried to do a is to build a PCRE regex to extract all images names with:

/File:(.*\..{3})/g

I've built this final command to find all occurrences in my .txt files and extract image name with my regex:

find . -name "*.txt" | xargs perl -i -p -e 's/File:(.*\..{3})/$1/g'

But, I've encountered some problems as:

  • An error:

xargs: unterminated quote

  • And finally I don't know how to use extracted image name to replace all occurrences (complete lines)

For a migration process, I need to do some replacement in my bash script.

So in my .txt files, I have these references for example:

[[File:My Image.png|400px|thumb|center|My Image]]
[[File:My Image.png|400px|thumb|center]]
[[File:My Image.png|400px|thumb]]
[[File:My Image.png|400px]]
[[File:My Image.png]]

What I need to do is to replace all these occurrences with this line (only - so no more size, description, or other informations):

[[File:My Image.png]]

So, I tried to do a is to build a PCRE regex to extract all images names with:

/File:(.*\..{3})/g

I've built this final command to find all occurrences in my .txt files and extract image name with my regex:

find . -name "*.txt" | xargs perl -i -p -e 's/File:(.*\..{3})/$1/g'

But, I've encountered some problems as:

  • An error:

xargs: unterminated quote

  • And finally I don't know how to use extracted image name to replace all occurrences (complete lines)

PS: I'm on MacOS system and use bash v4

deleted 26 characters in body; edited tags
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264

For a migration process, I need to do some replacement in my bash script.

So in my .txt files, I have these references for example:

[[File:My Image.png|400px|thumb|center|My Image]]
[[File:My Image.png|400px|thumb|center]]
[[File:My Image.png|400px|thumb]]
[[File:My Image.png|400px]]
[[File:My Image.png]]

What I need to do is to replace all these occurrences with this line (only - so no more size, description, or other informations):

[[File:My Image.png]]

So, what I tried to do a is to build a PCRE regex to extract all images names with:

/File:(.*\..{3})/g

I've buildedbuilt this final command to find all occurrences in my .txt files and extract image name with my regex:

find . -name "*.txt" | xargs perl -i -p -e 's/File:(.*\..{3})/$1/g'

But, I've encountered some problems as:

  • An error:

xargs: unterminated quote

  • And finally I don't know how to use extracted image name to replace all occurenciesoccurrences (complete lines)

Thank you for your help

For a migration process, I need to do some replacement in my bash script.

So in my .txt files, I have these references for example:

[[File:My Image.png|400px|thumb|center|My Image]]
[[File:My Image.png|400px|thumb|center]]
[[File:My Image.png|400px|thumb]]
[[File:My Image.png|400px]]
[[File:My Image.png]]

What I need to do is to replace all these occurrences with this line (only - so no more size, description, or other informations):

[[File:My Image.png]]

So, what I tried is to build a PCRE regex to extract all images names with:

/File:(.*\..{3})/g

I've builded this final command to find all occurrences in my .txt files and extract image name with my regex:

find . -name "*.txt" | xargs perl -i -p -e 's/File:(.*\..{3})/$1/g'

But, I've encountered some problems as:

  • An error:

xargs: unterminated quote

  • And finally I don't know how to use extracted image name to replace all occurencies (complete lines)

Thank you for your help

For a migration process, I need to do some replacement in my bash script.

So in my .txt files, I have these references for example:

[[File:My Image.png|400px|thumb|center|My Image]]
[[File:My Image.png|400px|thumb|center]]
[[File:My Image.png|400px|thumb]]
[[File:My Image.png|400px]]
[[File:My Image.png]]

What I need to do is to replace all these occurrences with this line (only - so no more size, description, or other informations):

[[File:My Image.png]]

So, I tried to do a is to build a PCRE regex to extract all images names with:

/File:(.*\..{3})/g

I've built this final command to find all occurrences in my .txt files and extract image name with my regex:

find . -name "*.txt" | xargs perl -i -p -e 's/File:(.*\..{3})/$1/g'

But, I've encountered some problems as:

  • An error:

xargs: unterminated quote

  • And finally I don't know how to use extracted image name to replace all occurrences (complete lines)
Source Link

Regex Pattern and Replace line with substitution

For a migration process, I need to do some replacement in my bash script.

So in my .txt files, I have these references for example:

[[File:My Image.png|400px|thumb|center|My Image]]
[[File:My Image.png|400px|thumb|center]]
[[File:My Image.png|400px|thumb]]
[[File:My Image.png|400px]]
[[File:My Image.png]]

What I need to do is to replace all these occurrences with this line (only - so no more size, description, or other informations):

[[File:My Image.png]]

So, what I tried is to build a PCRE regex to extract all images names with:

/File:(.*\..{3})/g

I've builded this final command to find all occurrences in my .txt files and extract image name with my regex:

find . -name "*.txt" | xargs perl -i -p -e 's/File:(.*\..{3})/$1/g'

But, I've encountered some problems as:

  • An error:

xargs: unterminated quote

  • And finally I don't know how to use extracted image name to replace all occurencies (complete lines)

Thank you for your help