-2

In my shell script, I've declared a variable 'result' which gives me output like this, ZLISTBGY|Guyana|D

Now, I have 3 more variables which work on the 'result' variable

argtbl=$(echo $result | cut -d"|" -f 1)

country=$(echo $result | cut -d"|" -f 2)

operation=$(echo $result | cut -d"|" -f 3)

So, the argtbl in the above example is "ZLISTBGY"

the country is "Guyana"

and, the operation is "D"


But when I get country names like "United Arab Emirates" or "American Samoa", basically the countries which are space separated, I do not get the desired value in the "result" variable

What I want is: ZLISTBAS|American Samoa|D

What I'm getting currently is: ZLISTBAS|American

I think the cut command for "country" variable considers the whitespaces in the country-names as delimeters/restrictions and doesn't proceed further

I have looked for solutions with awk/sed commands too, but none of them worked

I'd really appreciate if someone can help me with this.

ErikMD
  • 10,416
  • 2
  • 24
  • 55
  • Can you first try writing `echo "$result" ` or `printf '%s\n' "$result"` (with double quotes) instead of `echo $result` ? – ErikMD May 28 '22 at 14:55
  • @ErikMD Still doesn't solve the purpose. The result is yet the same – Navtesh Batra May 28 '22 at 15:01
  • 2
    Yes, quotes should be added in the `echo` but it's not causing this problem. If I do `result='ZLISTBAS|American Samoa|D'` in my shell, followed by `echo $result | cut -d"|" -f 2`, the output is `American Samoa` as expected. Please post a complete, minimal script that demonstrates the problem. – Thomas May 28 '22 at 15:03
  • Regarding `I think the cut command for "country" variable considers the whitespaces in the country-names as delimeters/restrictions` - no, it definitely does not. `cut` uses the 1 character you tell it to as a delimiter so when you do `cut -d'|'`, that `|` is the only delimiter that `cut` will use. – Ed Morton May 28 '22 at 15:21
  • @Thomas I'm sorry, the result for country name "American Samoa" is ZLISTBAS|American Samoa|A So, i've got 3 sql queries queryA & queryB -> insert queryC-> delete Now here's the part of the script, which might help: case ${operation} in A) tblnm=ABC typevt='A' pk_sql "$queryA" pk_sql "$queryB" ;; D) tblnm=XYZ typevt='D' pk_sql "$queryB" pk_sql "$queryC" ;; M) tblnm=ABC typevt='M' pk_sql "queryA" pk_sql "queryB" ;; esac – Navtesh Batra May 28 '22 at 15:25
  • 1
    `IFS=\| read -r argtbl country operation << – dan May 28 '22 at 15:28
  • 1
    The code you've given will not produce the output you're getting. `cut` considers only whichever delimiter is provided by `-d`, or a tab if no `-d`. – dan May 28 '22 at 15:31
  • This is how the values appear. If there's no issue with the "cut" command, then i'm not sure what is ZLISTBAL|Albania|A **ZLISTBAS|American** ZLISTBAD|Andorra|A ZLISTBAI|Anguilla|A **ZLISTBAG|Antigua** ZLISTBAW|Aruba|A ZLISTBBS|Bahamas|A – Navtesh Batra May 28 '22 at 16:14
  • 1
    You write `I do not get the desired value in the "result" variable`. Do I understand you correct and the problem occurs before you do anything with `cut`? In that case `cut` is not the problem but how you fill `result`. Can you show the code before `result=something`? – Walter A May 28 '22 at 18:37

0 Answers0