简体   繁体   中英

How to remove all new lines characters '\n' and format the script

I've script content like this example and I need to remove all '\\n' and format the script is regular format instead of wrapped lines.

#\n# Copyright (c) 2012   2016 by ABC. All rights reserved.\n#\n# This script handles the creation of database on a server.\n# Using db utility we are creating db for emp\n# db settings\n# we are starting the database.\n\n#\n# Program Name ...\n#\nPGM_NAME='createDB.sh'\n\n#\n# 

I tried using substring in VI editor like this but did not format the script.

:%s/\n//g

In vi regex, you need to escape backslashes and literal control characters. To escape backslashes you double them; to escape control characters, you precede them by CTRL-V .

So, to replace \\n with newlines, you can do:

:%s/\\\\n/ CTRL-V CTRL-M /g

Why not simply use a printf or echo -e with a command substitution to output the formatted script that can then be redirected to a new file if you choose? For example: printf "$(<script)" or echo -e $(<script)

You don't want to remove the newline characters, you want to expand the escape sequence.

Example Use/Output

With your original single-line in the file names script :

$ printf "$(<script)"
#
# Copyright (c) 2012   2016 by ABC. All rights reserved.
#
# This script handles the creation of database on a server.
# Using db utility we are creating db for emp
# db settings
# we are starting the database.

#
# Program Name ...
#
PGM_NAME='createDB.sh'

#
#

(the results with echo -e $(<script) are the same but it adds an additional '\\n' at the end -- which may be preferable -- up to you)

You can redirect the result to a new file of your choosing to capture the formatted script.

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