简体   繁体   English

通过单个命令将awk的管道输出传递给sed

[英]Pipe output of awk to sed in single command

I have a simple file like : 我有一个简单的文件,如:

a 123
b 234

In a second file, I want to replace a string by the value corresponding to a -> "123" in a single command . 在第二个文件中,我想用一个命令中与->“ 123”对应的值替换字符串。

Is there a way to pipe the result of grep "a" file1 | awk {print $2} 有没有一种方法来管道grep "a" file1 | awk {print $2}的结果? grep "a" file1 | awk {print $2} to sed s/string/my_output/g file2 grep "a" file1 | awk {print $2}sed s/string/my_output/g file2

Here is an awk 这是一个awk

cat file1
a 123
b 234

cat file2
cat
bad hat

awk -v FS="" 'FNR==NR {split($0,b," ");a[b[1]]=b[2];next} {for (i=1;i<=NF;i++) $i=a[$i]?a[$i]:$i}1' file1 file2
c 123 t
234 123 d   h 123 t

How does it work. 它是如何工作的。

awk -v FS="" '                      # By setting Field Separator to nothing, it works on one and one letter
FNR==NR {                           # This is true only for the first file (file1)
    split($0,arr-b," ")             # Split the data in first file (arr-b[1]=first filed, eks a, arr-b[2]=second field, eks 123
    arr-a[arr-b[1]]=arr-b[2]        # Store this to an array using field 1 as index. arr-a[a]=123 arr-a[b]=234 etc
    next}                           # Skip to next record in file1
    {                               # Run this for file2
    for (i=1;i<=NF;i++)             # Loop trough on by on letter "c" "a" "t"
        $i=arr-a[$i]?arr-a[$i]:$i}  # Test the letter one by one if its found in array "arr-a", if so use data from array, if not keep value
1                                   # Since this always will be true, do default action "print $0" (print every line)
' file1 file2                       # Read file1 and file2

If I interpret your question correctly, try 如果我正确解释了您的问题,请尝试

sed 's@^\(.*\) \(.*\)$@s/\1/\2/g@' SimpleFile > substitutions.file
sed -f substitutions.file 2ndFile > 2ndFile.tmp && mv 2ndFile.tmp 2ndFile

The first step will create a sed substitions file like 第一步将创建一个sed替换文件,例如

s/a/123/g
s/b/234/g

which when used as in line2 above, will substitute each a with 123, etc. 在上述第2行中使用时,将用123代替每个a,依此类推。

IHTH IHTH

You could do the following: 您可以执行以下操作:

grep a file1| awk '{print $2}' > tmp.txt

This will put '123' in a temporary file. 这会将“ 123”放入一个临时文件。 Then: 然后:

sed -f script.sed file2

Where the contents of script.sed is script.sed的内容在哪里

/string to replace/ {
    r tmp.txt
    d
}

This script replaces "string to replace" with the contents of tmp.txt. 此脚本用tmp.txt的内容替换“替换字符串”。

here's a pure-Awk solution. 这是一个纯Awk解决方案。 Put the dictionary in 'adict.dat' then pass your input file to the Awk command 将字典放在“ adict.dat”中,然后将输入文件传递给Awk命令

source 资源

BEGIN { 
    while(( getline line < "adict.dat" ) > 0 ) {
        $0 = line;
        myvars[$1] = $2;
    }
}

{
    for (varname in myvars) {
        gsub(varname, myvars[varname]);
    }
    print;
}

Example run 运行示例

/bin/echo 'a x b y' | gawk -f arepl.awk 

output 产量

123 x 234 y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM