Return Only A Portion Of A Line After Matching A Pattern

Lets say a file (fileName) contains the following data

abcdddddfffbbbbbdddStartPatternSomeData1EndPattern
abcdddddfffdfdfsdsfStartPatternSomeData2EndPattern
fsdfdsddfdffbbbbbdddStartPatternSomeData3EndPattern
abcdddfsdfsdfdsbdddStartPatternSomeData4EndPattern

and you are interested in

SomeData1

SomeData2

SomeData3

SomeData4

Following command Would help do that

cat fileName| sed -n -e  ‘s/^.*StartPattern//p’ | sed -n -e ‘s/EndPattern.*$//p’

Here :

  • -n means not to print anything by default.
  • -e is followed by a sed command.
  • s is the pattern replacement command.
  • The final p means to print the transformed line.
Advertisements