dnanar@debian:/tmp$ cat a | grep azerty| cut -d ' ' -f2 a b c d e f dnanar@debian:/tmp$ cat a | grep qwerty| cut -d ' ' -f2 g h i j k l #### And i would produce a file like that: a g b h c i d j e k f l