Pravin Satav Pravin Satav - 4 months ago 10
Bash Question

cut command issue with 2nd column

I m trying to cut certain valures from string. Its working as expected for the first column but when I m trying for second column its not giving desired output.

Code and output

[psatav]$ echo '$$JOB_ESTD_TIME1'="'2016-04-16 00:00:00' | '2016-04-16 20:00:00'| '2016-04-17 08:00:00'" | cut -d'|' -f1
$$JOB_ESTD_TIME1='2016-04-16 00:00:00'
[psatav]$ echo '$$JOB_ESTD_TIME2'="'2016-04-16 00:00:00' | '2016-04-16 20:00:00'| '2016-04-17 08:00:00'" | cut -d'|' -f2
'2016-04-16 20:00:00'


Second command should return :-

$$JOB_ESTD_TIME2='2016-04-16 20:00:00'

Answer

What you are doing is passing $$JOB_ESTD_TIME1='2016-04-16 00:00:00' | '2016-04-16 20:00:00'| '2016-04-17 08:00:00' this whole string to cut so for f1 it cuts

$$JOB_ESTD_TIME1='2016-04-16 00:00:00'

and for f2 it cuts only '2016-04-16 20:00:00'

If you don't want to use awk and stick to cut here is what you should do

echo '$$JOB_ESTD_TIME2'=$(echo "'2016-04-16 00:00:00' | '2016-04-16 20:00:00'| '2016-04-17 08:00:00'" | cut -d'|' -f2)
$$JOB_ESTD_TIME2= '2016-04-16 20:00:00'

echo '$$JOB_ESTD_TIME2'=$(echo "'2016-04-16 00:00:00' | '2016-04-16 20:00:00'| '2016-04-17 08:00:00'" | cut -d'|' -f1)
$$JOB_ESTD_TIME2='2016-04-16 00:00:00'