awk管道匹配的内容

时间:2017-02-15 01:57:38

标签: shell awk grep

我使用awk,grep使用管道获取我们称为A的内容(我文件中的部分内容):

_O2 int381,int382,int384,int385,int386,int387,int388,int391,int392,int393,int394,int395,int396,int397,int398,int399,int400,int401,int402,int403,int404,int408,int409,int410,int412,int413,int414:chr4:31119012..31944575    chr4:31669055..31674598 LOC_Os04g53190,LOC_Os04g53195   CPuORF12,expressed - conserved peptide uORF-containing transcript, expressed ; protein ;        PF01593 Amino_oxidase   0.0539946

我想使用内容来grep或获取匹配的内容和B中的其他内容(我文件中的部分内容):

cat a|awk -F"," '{for (i=1;i<=NF;i++)print $i}'|grep -f - B|grep PF|awk '{print $4"\t"$(NF-2)}'

当我使用

LOC_Os04g53190,LOC_Os04g53195   PF01593

我会得到

  LOC_Os04g53190 PF01593
  LOC_Os04g53195 PF01593

但是,我想打印

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/*Copyright (c) 2011 Aravind Rao
Modifications by Sam Barnum, 360Works 2012
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 * to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

public final class ASCII {
    boolean negative;

    public ASCII() {
        this(false);
    }

    public ASCII(final boolean negative) {
        this.negative = negative;
    }

    public String convert(final BufferedImage image) {
        StringBuilder sb = new StringBuilder((image.getWidth() + 1) * image.getHeight());
        for (int y = 0; y < image.getHeight(); y++) {
            if (sb.length() != 0) sb.append("\n");
            for (int x = 0; x < image.getWidth(); x++) {
                Color pixelColor = new Color(image.getRGB(x, y));
                double gValue = (double) pixelColor.getRed() * 0.2989 + (double) pixelColor.getBlue() * 0.5870 + (double) pixelColor.getGreen() * 0.1140;
                final char s = negative ? returnStrNeg(gValue) : returnStrPos(gValue);
                sb.append(s);
            }
        }
        return sb.toString();
    }

    /**
     * Create a new string and assign to it a string based on the grayscale value.
     * If the grayscale value is very high, the pixel is very bright and assign characters
     * such as . and , that do not appear very dark. If the grayscale value is very lowm the pixel is very dark,
     * assign characters such as # and @ which appear very dark.
     *
     * @param g grayscale
     * @return char
     */
    private char returnStrPos(double g)//takes the grayscale value as parameter
    {
        final char str;

        if (g >= 230.0) {
            str = ' ';
        } else if (g >= 200.0) {
            str = '.';
        } else if (g >= 180.0) {
            str = '*';
        } else if (g >= 160.0) {
            str = ':';
        } else if (g >= 130.0) {
            str = 'o';
        } else if (g >= 100.0) {
            str = '&';
        } else if (g >= 70.0) {
            str = '8';
        } else if (g >= 50.0) {
            str = '#';
        } else {
            str = '@';
        }
        return str; // return the character

    }

    /**
     * Same method as above, except it reverses the darkness of the pixel. A dark pixel is given a light character and vice versa.
     *
     * @param g grayscale
     * @return char
     */
    private char returnStrNeg(double g) {
        final char str;

        if (g >= 230.0) {
            str = '@';
        } else if (g >= 200.0) {
            str = '#';
        } else if (g >= 180.0) {
            str = '8';
        } else if (g >= 160.0) {
            str = '&';
        } else if (g >= 130.0) {
            str = 'o';
        } else if (g >= 100.0) {
            str = ':';
        } else if (g >= 70.0) {
            str = '*';
        } else if (g >= 50.0) {
            str = '.';
        } else {
            str = ' ';
        }
        return str;

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "gif", "png"));
                while (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    try {
                        File f = fileChooser.getSelectedFile();
                        final BufferedImage image = ImageIO.read(f);
                        if (image == null) throw new IllegalArgumentException(f + " is not a valid image.");
                        final String ascii = new ASCII().convert(image);
                        final JTextArea textArea = new JTextArea(ascii, image.getHeight(), image.getWidth());
                        textArea.setFont(new Font("Monospaced", Font.BOLD, 5));
                        textArea.setEditable(false);
                        final JDialog dialog = new JOptionPane(new JScrollPane(textArea), JOptionPane.PLAIN_MESSAGE).createDialog(ASCII.class.getName());
                        dialog.setResizable(true);
                        dialog.setVisible(true);
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
                System.exit(0);
            }
        });
    }

}

2 个答案:

答案 0 :(得分:1)

改进awk最后声明

cat a | 
awk -F"," '{for (i=1;i<=NF;i++)print $i}' | 
grep -f - B | 
grep PF | 
awk '{n=split($4,v,","); for(i=1; i<=n; ++i) print v[i]"\t"$(NF-2)}'

你明白了,

LOC_Os04g53190  PF01593
LOC_Os04g53195  PF01593

奖励:awk only solution

awk '
    NR==FNR{d[$1]; next}
    $(NF-2) ~ /^PF/{
        n=split($4,v,",")
        for(i=1; i<=n; ++i) if(v[i] in d) print v[i]"\t"$(NF-2)
    }
' RS="[\n,]" a RS="\n" B

答案 1 :(得分:0)

示例文件

sharad$ cat sample_file
foo
bar
sharad$ 

将匹配内容捕获到变量

sharad$ match=$(cat sample_file | grep foo)

将不匹配的内容捕获到另一个变量

sharad$ non_match=$(cat sample_file | grep -v foo)
sharad$ 

验证匹配和非匹配变量的内容(grep -v)

sharad$ echo $match
foo
sharad$ echo $non_match
bar
sharad$

来自man grep

-v, - 反转匹配          选定的行是不匹配任何指定模式的行。

相关问题