I am currently writing a script that accesses another file using “mapfile” to put it into an array. However, the inputted file contains some white spaces, which the script reads as a new array entry.
How do I replace the white spaces with underscores within the body of the file I’m bringing in? I have only seen instructions for how to replace a file name, not what’s within it.
Thank you in advance! Please let me know if you need any further clarification.
Advertisement
Answer
Given:
$ cat file 1 2 3 4
You can use sed
:
$ sed 's/[[:space:]]/_/g' file 1_2_3_4
So then you can do:
$ mapfile -t arr < <(sed 's/[[:space:]]/_/g' file)