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

Par*_*ora 5 bash grep

这是一个脚本,它从文件的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, $2, ...
    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

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

Pau*_*ce. 9

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

#!/bin/bash
echo
read -r -p "Enter the words to be translated: " a
echo $(printf "%s\n" $a | grep -Ff - replaced.txt | cut -f 2 -d ' ')
Run Code Online (Sandbox Code Playgroud)

不需要循环.

所述echo具有无引号参数删除嵌入的新行和替代多个空间和/或突出部的每个序列与一个空间.