Search code examples
unixsedgrepsolariscut

How to cut from one position to another , if 2 positions having a match in unix


I have a file like this

ABCDEFGH  
IJKLMNOP
QRSTUFWH

if 6th position = F and if 8th position = H I want to cut from position 2 to 4

So the output should be

BCD
RST

I can take records with the matching pattern to another file using this -

grep  '^.....F.H' f1.txt > f2.txt

What i want is only position 2 to 4 , which matches the pattern. Please help Thank you


Solution

  • Could you please try following.

    awk 'substr($0,6,1)=="F" && substr($0,8,1)=="H"{print substr($0,2,3)}' Input_file
    

    Since you added Solaris tag in your question try changing awk to /usr/xpg4/bin/awk in case you are on Solaris.