Search code examples
bashprintingdelimitercut

Print text after every ! (exclamation mark) in bash


I would like print strings/texts after every ! (exclamation mark) in bash.

Example string/text:

check_queue!TEST_IN!Queue!400!750

I want this output:

TEST_IN Queue 400 750

I have tried this:

cat filename | cut -d "!" -f2

Solution

  • You were almost there:

    cut -d! -f2- filename | tr '!' ' '
    

    -f2- means field 2 and all following fields

    No need for cat, just work on file

    tr '!' ' ' translates exclamation mark ! to space .

    Or if your version of cut has an --output-delimiter= option:

    cut --delimiter=! --fields=2- --output-delimiter=' ' filename
    

    Or using awk:

    awk -F! '{$1=""; print substr($0,2)}' filename
    
    • -F!: Sets the field delimiter to !
    • $1="": Erase first field
    • print substr($0,2): Print the whole record starting at 2nd character, since first one is blank delimiter remain from erased first field.