-2

We have a string as below:

**XX_EMAIL_FILES FCP_REQID=9614696 FCP_LOGIN="APPS/sup12" FCP_USERID=5667 FCP_USERNAME="SRI" FCP_PRINTER="noprint" FCP_SAVE_OUT=Y FCP_NUM_COPIES=1 "9614556_SUP12_XX_Workflow_Stuck_AP_Invoices.csv" "/tmp_mnt2/attachments" "Sri.B@xx.com" "This is the subject for the mail" "PFA for the list of Invoices that are stuck in workflow."**

We need to fetch only the below string into a variable:

This is the subject for the mail

Similarly, need to fetch only the below string into other variable: PFA for the list of Invoices that are stuck in workflow.

These are not always the same. the strings may vary based on what user entered.

Can anyone please help me on how to fetch only those strings using UNIX commands

Regards, Bommi

jsotola
  • 440
Bommi
  • 25
  • is the format consistent across all data? – jsotola Aug 14 '19 at 05:49
  • format is consistent. But, some times it is like This is the subject for the mail, sometimes it may like This is the subject for the mail. With more subject and sometimes it is just NULL value – Bommi Aug 14 '19 at 05:52
  • 1
    To make things more readable, I'd suggest you to format as code any text that has to be fed as input/matched literally/read by a program. It will help avoiding ambiguities, e.g. are flanking ** part of the original string? – fra-san Aug 14 '19 at 06:43
  • Use e.g. sed(1) to isolate the required substring. – vonbrand Aug 14 '19 at 13:31

1 Answers1

0

If you sure about number and order of fields and quotes in them, then you could use cut -d\" to get required fields like this:

echo 'XX_EMAIL_FILES FCP_REQID=9614696 FCP_LOGIN="APPS/sup12" FCP_USERID=5667 FCP_USERNAME="SRI" FCP_PRINTER="noprint" FCP_SAVE_OUT=Y FCP_NUM_COPIES=1 "9614556_SUP12_XX_Workflow_Stuck_AP_Invoices.csv" "/tmp_mnt2/attachments" "Sri.B@xx.com" "This is the subject for the mail" "PFA for the list of Invoices that are stuck in workflow."' \
| cut -f14 -d\"
This is the subject for the mail

echo 'XX_EMAIL_FILES FCP_REQID=9614696 FCP_LOGIN="APPS/sup12" FCP_USERID=5667 FCP_USERNAME="SRI" FCP_PRINTER="noprint" FCP_SAVE_OUT=Y FCP_NUM_COPIES=1 "9614556_SUP12_XX_Workflow_Stuck_AP_Invoices.csv" "/tmp_mnt2/attachments" "Sri.B@xx.com" "This is the subject for the mail" "PFA for the list of Invoices that are stuck in workflow."' \
| cut -f16 -d\"
PFA for the list of Invoices that are stuck in workflow.

Or may be even better to use awk if you need to get last two fields:

echo 'XX_EMAIL_FILES FCP_REQID=9614696 FCP_LOGIN="APPS/sup12" FCP_USERID=5667 FCP_USERNAME="SRI" FCP_PRINTER="noprint" FCP_SAVE_OUT=Y FCP_NUM_COPIES=1 "9614556_SUP12_XX_Workflow_Stuck_AP_Invoices.csv" "/tmp_mnt2/attachments" "Sri.B@xx.com" "This is the subject for the mail" "PFA for the list of Invoices that are stuck in workflow."' \
| awk -F\" '{ print $(NF-3) }'
This is the subject for the mail
  • I used cut -f14 -d". It is ok. But now facing an other issue

    Actually, we developed host file (using UNIX) to send emails. Here, user provide the subject and body while running the programs (Oracle Concurrent Programs).

    After fetching the subject and body values using above commands, the values are stores in variables as below:

    FCP_SUBJECT as This is the subject and FCP_BODY as This is the body

    Now, mailing as below:

    echo "Hello,

    ${FCP_BODY}

    Thanks, ${FCP_USERNAME}

    "| mailx -s$FCP_SUBJECT

    Then, the mail subject is coming as just This

    – Bommi Aug 14 '19 at 06:25
  • @Bommi, that is a different question from what you asked above .... post a new question – jsotola Aug 14 '19 at 06:41
  • 1
    @Bommi If you run mailx -s$FCP_SUBJECT, the variable FCP_SUBJECT undergoes splitting and its first word only is used as the argument value to -s. Pay attention to quoting your variables. – fra-san Aug 14 '19 at 06:50