简体   繁体   中英

Replace white spaces with underscores within a file (bash)

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.

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)

与tr:

tr [[:space:]] '_' <filename

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM