0

We have a value

XX_EMAIL_GL_BTCH_ATTACH FCP_REQID=8830260 FCP_LOGIN="APPS/pwd" FCP_USERID=4807 FCP_USERNAME="USER" FCP_PRINTER="noprint" FCP_SAVE_OUT=Y FCP_NUM_COPIES=1 "XX WBP 8-501 Misc Receipts" "xyz.mno@abc.com"

We need to fetch the value XX WBP 8-501 Misc Receipts.

We tried as below, but it is returning only XX.

echo XX_EMAIL_GL_BTCH_ATTACH FCP_REQID=8830260 FCP_LOGIN="APPS/pwd" FCP_USERID=4807 FCP_USERNAME="USER" FCP_PRINTER="noprint" FCP_SAVE_OUT=Y FCP_NUM_COPIES=1 "XX WBP 8-501 Misc Receipts" "xyz.mno@abc.com" | cut -d" " -f9 | sed 's/"//g'
  • Is the value in a file, or a shell variable, or ??? – Jeff Schaller Nov 2 '17 at 13:04
  • cut -d \" -f 8 < sample would do it on your sample. – Stéphane Chazelas Nov 2 '17 at 13:07
2

Following your pattern you just need to add some columns to cut:

cut -d' ' -f9-13 | sed 's/"//g'

You could also use a pure sed solution:

sed 's/.*"\(.*\)" ".*"$/\1/'

The pattern depends on two double-quoted strings at the end. With that it displays the first of the two (or IOW the second to last double-quoted sub-string from the full string).

Update: With echo as OP seems to want...

echo 'XX_EMAIL_GL_BTCH_ATTACH FCP_REQID=8830260 FCP_LOGIN="APPS/pwd" FCP_USERID=4807 FCP_USERNAME="USER" FCP_PRINTER="noprint" FCP_SAVE_OUT=Y FCP_NUM_COPIES=1 "XX WBP 8-501 Misc Receipts" "xyz.mno@abc.com"' | sed 's/.*"\(.*\)" ".*"$/\1/'
  • Hi,f9-13 will not always work.... the value may vary. So, what I need is to fetch the value which is in that double quotes – Bommi Nov 2 '17 at 12:34
  • You need to add all relevant details like this to your question. Anyways, the sed-only solution should work. – B Layer Nov 2 '17 at 12:36
  • Hi, could you please let me know how to achieve this..... As I said before, sed is fetching only XX, but I need complete XX WBP 8-501 Misc Receipts – Bommi Nov 2 '17 at 12:38
  • I took the full string from your question and put it in a file and then did sed '...' file and I get XX WBP 8-501 Misc Receipts. Are you sure you're typing it in correctly? – B Layer Nov 2 '17 at 12:40
  • If you don't want to put it in a file you have to surround the string with (single) quotes as I show in the update to my answer. – B Layer Nov 2 '17 at 12:42
1

Please note that the way you echo the string, the shell will eat the double quotes, so you are not able to tell how far your ninth field goes. You you need to surround your string with single quotes first.

Then you have to consider that cut is just looking for spaces as delimiters and doesn't know how to ignore spaces inside of quotes, so do it this way:

echo 'XX_EMAIL_GL_BTCH_ATTACH FCP_REQID=8830260 FCP_LOGIN="APPS/pwd" FCP_USERID=4807 FCP_USERNAME="USER" FCP_PRINTER="noprint" FCP_SAVE_OUT=Y FCP_NUM_COPIES=1 "XX WBP 8-501 Misc Receipts" "xyz.mno@abc.com"' | cut -d" " -f9- | cut -d\" -f 2

The first cut keeps everything from the ninth field on, the second takes " as delimiter to keep just the part inside the first pair of quotes.

With pure sed the same could be done with

sed -E 's/([^ ]* ){8}//;s/"//;s/".*//'

In any case, you can break the script if you'll add spaces to earlier column fields.

1

You won't get a robust solution using cut while extracting logical substrings from such kind of input.

Use one of the following solid solutions:

1) Python command (simplified):

python -c 'import csv,sys; r=csv.reader(sys.stdin, delimiter=" "); print(next(r)[8])' <file

Python's csv module id pretty good to read and write tabular data.


2) GNU awk command:

awk -v FPAT='"[^"]+"|[^"]+' '{ gsub("\"","",$8); print $8 }' file

The output for both approaches:

XX WBP 8-501 Misc Receipts

  • Considering the resistance to using only sed this is a long shot. ;) Seems OP really wants a solution with cut despite the fact that it won't work since the length of the string to cut can vary. – B Layer Nov 2 '17 at 12:47
  • @BLayer, the input string is NOT suitable to split by whitespaces, it has an evident csv structure - i.e. words with spaces enclosed with quotes .. FCP_NUM_COPIES=1 "XX WBP 8-501 Misc Receipts" "xyz.mno@abc.com" – RomanPerekhrest Nov 2 '17 at 12:50
  • Preaching to the choir. Read my comments below my answer. – B Layer Nov 2 '17 at 12:51
  • @BLayer, yes, your approach is not for complex csv data – RomanPerekhrest Nov 2 '17 at 12:53
  • I think you're missing my point....which is simply that OP is intent on using cut rather than sed or Python or whatever. – B Layer Nov 2 '17 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.