Find/Replace special characters in text file using Bash script

zotteken

I'm looking for some guidance on creating a script to find and replace special characters inside a text file.

I've come up with this piece of pseudo code but filling in the blanks is a bit harder:

  • Find newline & replace by space.
  • Find CP & replace by newline.
  • Find Mr. Mime (with space) & replace by Mr.Mime (without space)
  • Find tab & replace by space
  • Find double space & replace by single space
  • Find % & replace with nothing (aka just leave it out)
  • Find " ATK DEF STA IV " & replace by space

"Find" stands for "Find All Instances".

I've been looking into sed, but I can't seem to find how I'd handle these special characters. Any ideas much appreciated.

EDIT: As asked hereby an little snippet of the input:

CP 1593
SSS
Sudowoodo♀
ATK     DEF     STA     IV
15  15  15  100.0%
counter
rock slide
CP 1262
SSS
Tangrowth♀4
ATK     DEF     STA     IV
15  15  15  100.0%
vine whip
grass knot
CP 1077
SSS
Mr. Mime♀
ATK     DEF     STA     IV
15  15  15  100.0%
confusion
psychic

And the expected output:

1593 SSS Sudowoodo♀ 15 15 15 100.0 counter rock slide
1262 SSS Tangrowth♀4 15 15 15 100.0 vine whip grass knot
1077 SSS Mr.Mime♀ 15 15 15 100.0 confusion psychic
glenn jackman

sed text process is strictly line oriented, so it's pretty difficult to replace newlines with sed.
Untested:

cat file |
  tr '\n\t' '  ' |
  sed -e 's/ CP /\n/g' \
      -e 's/Mr[.] Mime/Mr.Mime/g' \
      -e 's/   */ /g' \
      -e 's/%//g'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replacing special characters in text file using PowerShell

From Dev

Bash: Insert special characters text after string in text file

From Dev

echo special characters to a file in bash

From Dev

special characters (emoticons) in text file

From Dev

Remove special characters in a text file

From Dev

using bash script to sort text in a file

From Dev

pass special characters from input to bash script

From Dev

Bash script and escaping special characters in password

From Dev

Shell Script (Bash): Omits special characters

From Dev

Bash script and escaping special characters in password

From Dev

Can't echo or create a symbolic link to a file with special characters in its name inside bash script

From Dev

Bash scripting, write special characters to file

From Dev

replace special characters in a text file batch

From Dev

Insert text in specific lines of a file? with special characters

From Dev

Is there a way to remove special characters from a text file?

From Dev

Write to text file but escape special characters

From Dev

Getting text between special characters using Regex

From Dev

Create a file using special characters as filename

From Dev

Editing nested text and specific lines within a file using bash script

From Dev

single text file on require multiple operations using a shell or bash script

From Dev

How can I create a file with some text in it using bash script

From Dev

Using a bash script to write current date to a .cfg/text file

From Dev

BASH: How to copy the name of the file and insert it into the text using script?

From Dev

Output the current PATH to text file using BASH script

From Dev

How to create dynamic headers on a text file using BASH script

From Dev

Edit multiple lines of text file using bash script

From Dev

finding special characters in array of string variables bash script

From Dev

How do I handle special characters like a bracket in a bash script?

From Dev

Best practice to read password, containing special characters like $!@`', into Bash script

Related Related

  1. 1

    Replacing special characters in text file using PowerShell

  2. 2

    Bash: Insert special characters text after string in text file

  3. 3

    echo special characters to a file in bash

  4. 4

    special characters (emoticons) in text file

  5. 5

    Remove special characters in a text file

  6. 6

    using bash script to sort text in a file

  7. 7

    pass special characters from input to bash script

  8. 8

    Bash script and escaping special characters in password

  9. 9

    Shell Script (Bash): Omits special characters

  10. 10

    Bash script and escaping special characters in password

  11. 11

    Can't echo or create a symbolic link to a file with special characters in its name inside bash script

  12. 12

    Bash scripting, write special characters to file

  13. 13

    replace special characters in a text file batch

  14. 14

    Insert text in specific lines of a file? with special characters

  15. 15

    Is there a way to remove special characters from a text file?

  16. 16

    Write to text file but escape special characters

  17. 17

    Getting text between special characters using Regex

  18. 18

    Create a file using special characters as filename

  19. 19

    Editing nested text and specific lines within a file using bash script

  20. 20

    single text file on require multiple operations using a shell or bash script

  21. 21

    How can I create a file with some text in it using bash script

  22. 22

    Using a bash script to write current date to a .cfg/text file

  23. 23

    BASH: How to copy the name of the file and insert it into the text using script?

  24. 24

    Output the current PATH to text file using BASH script

  25. 25

    How to create dynamic headers on a text file using BASH script

  26. 26

    Edit multiple lines of text file using bash script

  27. 27

    finding special characters in array of string variables bash script

  28. 28

    How do I handle special characters like a bracket in a bash script?

  29. 29

    Best practice to read password, containing special characters like $!@`', into Bash script

HotTag

Archive