Print text after every ! (exclamation mark) in bash

Viewed 69

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
2 Answers

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.

Fist apply a FOREACH line loop

while read s; do
  #foreach substring 
    for (( i=0; i<${#s}; i++ )); do
if ["${s:$i:1}" != “!”] then
   #add to the String the extra letter 
   String="${s} ${s:$i:1}"
else
    #print each time you find !
    print String 
    #prepare it to print next String
    String =“”
fi 
done
  done <your filename.txt
Related