如何在bash脚本中的特殊字符后添加空格?

发布于 2025-01-11 04:11:31 字数 449 浏览 0 评论 0原文

我有一个文本文件,其中包含

!aa
@bb
#cc
$dd
%ee

预期输出,

! aa
@ bb
# cc
$ dd
% ee

我尝试过的内容,echo "${foo//@/@ }"

这对于一个字符串确实可以正常工作,但不适用于文件中的所有行。我尝试使用这个 while 循环来读取文件的所有行并使用 echo 执行相同的操作,但它不起作用。

while IFS= read -r line; do
    foo=$line
    sep="!@#$%"
    echo "${foo//$sep/$sep }"
done < $1

我尝试过 awk split 但它没有给出预期的输出。有什么解决方法吗?通过使用 awk 或 sed。

I have a text file with something like,

!aa
@bb
#cc
$dd
%ee

expected output is,

! aa
@ bb
# cc
$ dd
% ee

What I have tried, echo "${foo//@/@ }".

This does work fine with one string but it does not work for all the lines in the file. I have tried with this while loop to read all the lines of the file and do the same using echo but it does not work.

while IFS= read -r line; do
    foo=$line
    sep="!@#$%"
    echo "${foo//$sep/$sep }"
done < $1

I have tried with awk split but it does not give the expected output. Is there any workaround for this? by using awk or sed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等往事风中吹 2025-01-18 04:11:31

以下假设您要在 !@#$% 集中的每个字符后面添加一个空格(即使它是一行中的最后一个字符)。测试文件:

$ cat file.txt
a!a
@bb
c#c
$dd
ee%
foo
%b%r
$ sep='!@#$%'

使用sed

$ sed 's/['"$sep"']/& /g' file.txt
a! a
@ bb
c# c
$ dd
ee% 
foo
% b% r

使用awk

$ awk '{gsub(/['"$sep"']/,"& "); print}' file.txt
a! a
@ bb
c# c
$ dd
ee% 
foo
% b% r

使用普通bash(不推荐,太慢):

$ while IFS= read -r line; do
    str=""
    for (( i=0; i<${#line}; i++ )); do
      char="${line:i:1}"      
      str="$str$char"
      [[ "$char" =~ [$sep] ]] && str="$str "
    done
    printf '%s\n' "$str"
  done < file.txt
a! a
@ bb
c# c
$ dd
ee% 
foo
% b% r

或者(不确定哪个是最差):

$ while IFS= read -r line; do
    for (( i=0; i<${#sep}; i++ )); do
      char="${sep:i:1}"
      line="${line//$char/$char }"
    done
    printf '%s\n' "$line"
  done < file.txt
a! a
@ bb
c# c
$ dd
ee% 
foo
% b% r

The following assumes you want to add a space after every character in the !@#$% set (even if it is the last character in a line). Test file:

$ cat file.txt
a!a
@bb
c#c
$dd
ee%
foo
%b%r
$ sep='!@#$%'

With sed:

$ sed 's/['"$sep"']/& /g' file.txt
a! a
@ bb
c# c
$ dd
ee% 
foo
% b% r

With awk:

$ awk '{gsub(/['"$sep"']/,"& "); print}' file.txt
a! a
@ bb
c# c
$ dd
ee% 
foo
% b% r

With plain bash (not recommended, it is too slow):

$ while IFS= read -r line; do
    str=""
    for (( i=0; i<${#line}; i++ )); do
      char="${line:i:1}"      
      str="$str$char"
      [[ "$char" =~ [$sep] ]] && str="$str "
    done
    printf '%s\n' "$str"
  done < file.txt
a! a
@ bb
c# c
$ dd
ee% 
foo
% b% r

Or (not sure which is the worst):

$ while IFS= read -r line; do
    for (( i=0; i<${#sep}; i++ )); do
      char="${sep:i:1}"
      line="${line//$char/$char }"
    done
    printf '%s\n' "$line"
  done < file.txt
a! a
@ bb
c# c
$ dd
ee% 
foo
% b% r
最终幸福 2025-01-18 04:11:31

在您的示例中,您称为 special 的字符似乎是 GNU sed 中称为 [[:punct:]] 的字符子集,因此我建议以下解决方案:

sed 's/\([[:punct:]]\)/\1 /g' file.txt

file.txt 内容

!aa
@bb
#cc
$dd
%ee

输出

! aa
@ bb
# cc
$ dd
% ee

说明:我使用捕获组 \(...\) ,其中包含任何属于的字符到 [:punct:] 然后我替换捕获的内容捕获的内容后跟空格。我使用 g 将其应用于每行中的所有事件,尽管这对上面的数据没有明显的影响。如果您确定每一行中最多有一个字符需要替换,您可以选择删除 g

如果您想了解有关 [:punct:] 或其他类似字符集的更多信息,请阅读 Regular-Expressions.info 上的字符类

Characters you call special in your example seems to be subset of characters known as [[:punct:]] to GNU sed, thus I propose following solution:

sed 's/\([[:punct:]]\)/\1 /g' file.txt

which with file.txt content being

!aa
@bb
#cc
$dd
%ee

output

! aa
@ bb
# cc
$ dd
% ee

Explanation: I use capturing group \(...\) which has any character belonging to [:punct:] then I replace what was captured with content of that capture followed by space. I use g to apply it to all occurences in each line, though this has not visible impact for data above. You might elect to drop g if you are sure there will be at most one character to replace in every line.

If you want to know more about [:punct:] or other similar character sets read about Character Classes on Regular-Expressions.info

烏雲後面有陽光 2025-01-18 04:11:31

如果文件总是在行开头包含这样的符号,则使用此

sed -Ei 's/^(.)/\1 /g' yourfile.txt

-E 选项是告诉 sed 使用正则表达式。 -i 内联修改文件,如果要输出到控制台或其他文件,可以将其删除。 ^(.) 正则表达式捕获该行的第一个字符并为其添加一个空格 (\1)

If the file always contain a symbol at the start of line like that then use this

sed -Ei 's/^(.)/\1 /g' yourfile.txt

The -E option is to tell sed to use regex. -i modifies the file inline, you can remove it if you want to output to console or another file. The ^(.) regex captures the first character on the line and add a space to it (\1)

套路撩心 2025-01-18 04:11:31

假设特殊字符是非数字和非字母字符,并且特殊字符可以出现在行中的任何位置,请使用以下正则表达式来替换它们。

sed 's/[^a-zA-Z0-9]/& /g' urfile

Assuming that special characters are non-numeric and non-alphabetic characters, and special characters can appear anywhere in the line, use the following regular expression to replace them.

sed 's/[^a-zA-Z0-9]/& /g' urfile
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文