加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 如何在shell脚本中以单行输出grep?

发布时间:2020-12-15 22:38:39 所属栏目:安全 来源:网络整理
导读:这是一个脚本,它从文件的replace.txt中读取单词并显示每行中每个单词的输出,但我想在一行中显示所有输出. #!/bin/sh echo echo "Enter the word to be translated" read aIFS=" " # Set the field separatorset $a # Breaks the string into $1,$2,...for a
这是一个脚本,它从文件的replace.txt中读取单词并显示每行中每个单词的输出,但我想在一行中显示所有输出.

#!/bin/sh
 echo
 echo "Enter the word to be translated"
 read a
IFS=" "     # Set the field separator
set $a      # Breaks the string into $1,$2,...
for a    # a for loop by default loop through $1,...
    do
    {
    b= grep "$a" replaced.txt | cut -f 2 -d" " 
    }
    done 

“replacement.txt”文件的内容如下:

hllo HELLO
m AM
rshbh RISHABH
jn JAIN
hw HOW 
ws WAS 
ur YOUR
dy DAY

这个问题不适合我的问题,我只需要帮助就可以将脚本的输出放在一行中.

解决方法

您的整个脚本可以替换为:

#!/bin/bash
echo
read -r -p "Enter the words to be translated: " a
echo $(printf "%sn" $a | grep -Ff - replaced.txt | cut -f 2 -d ' ')

不需要循环.

带有不带引号的参数的echo将删除嵌入的换行符,并用一个空格替换多个空格和/或制表符的每个序列.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读