Search code examples
linuxunixgrepcut

How to print a range of character in unix using cut and grep?


I can print block of characters using (cut -d) specifying delimiter.

# grep -i access.log| grep ' 500 '| cut -d\- -f1 

But how can I print characters after specific pattern "address=tel%3A%2B"

For example: how to print the entire phone numbers in this file?

begining of the line address=tel%3A%2B416xxxxxxx rest of the log
begining of the line address=tel%3A%2B647xxxxxxx rest of the log
begining of the line address=tel%3A%2B519xxxxxxx rest of the log

...


Solution

  • sed -e 's,.*address=tel%3A%2B\([^ ]\+\).*,\1,g' <<EOF                          
    begining of the line address=tel%3A%2B416xxxxxxx rest of the log                
    begining of the line address=tel%3A%2B647xxxxxxx rest of the log                
    begining of the line address=tel%3A%2B519xxxxxxx rest of the log                
    EOF
    

    Output:

    416xxxxxxx
    647xxxxxxx
    519xxxxxxx