bash 如何在shell脚本中以单行形式获取grep的输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10724900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:08:30  来源:igfitidea点击:

How to get output of grep in single line in shell script?

bashgrep

提问by ParveenArora

Here is a script which reads words from the file replaced.txt and displays the output each word in each line, But I want to display all the outputs in a single line.

这是一个脚本,它从文件replaced.txt 中读取单词并在每行中显示每个单词的输出,但我想在一行中显示所有输出。

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

Content of "replaced.txt" file is given below:

“replaced.txt”文件内容如下:

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

This question can't be appropriate to what I asked, I just need the help to put output of the script in a single line.

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

回答by Paused until further notice.

Your entire script can be replaced by:

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

#!/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 ' ')

No need for a loop.

不需要循环。

The echowith an unquoted argument removes embedded newlines and replaces each sequence of multiple spaces and/or tabs with one space.

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

回答by ruakh

One hackish-but-simple way to remove trailing newlines from the output of a command is to wrap it in printf %s "$(...) ". That is, you can change this:

从命令的输出中删除尾随换行符的一种黑客但简单的方法是将其包装在printf %s "$(...) ". 也就是说,你可以改变这个:

b= grep "$a" replaced.txt | cut -f 2 -d" "

to this:

对此:

printf %s "$(grep "$a" replaced.txt | cut -f 2 -d" ") "

and add an echocommand after the loop completes.

echo在循环完成后添加一个命令。

The $(...)notation sets up a "command substitution": the command grep "$a" replaced.txt | cut -f 2 -d" "is run in a subshell, and its output, minus any trailing newlines, is substituted into the argument-list. So, for example, if the command outputs DAY, then the above is equivalent to this:

$(...)符号设置了“命令替换”:命令grep "$a" replaced.txt | cut -f 2 -d" "在子shell 中运行,其输出(减去任何尾随换行符)被替换到参数列表中。因此,例如,如果命令输出DAY,则上述内容等效于:

printf %s "DAY "

(The printf %s ...notation is equivalent to echo -n ...— it outputs a string without adding a trailing newline — except that its behavior is more portably consistent, and it won't misbehave if the string you want to print happens to start with -nor -eor whatnot.)

(该printf %s ...符号等效于echo -n ...- 它输出一个字符串而不添加尾随换行符 - 除了它的行为更可移植地一致,并且如果您要打印的字符串碰巧以-nor-e或诸如此类的开头,它也不会出现错误行为。)

回答by NamertaArora

You can also use

你也可以使用

awk 'BEGIN { OFS=": "; ORS=" "; } NF >= 2 { print ; }'

in a pipe after the cut.

在切割后的管道中。