Pavan Singh Pavan Singh - 1 year ago 66
Linux Question

How to eliminate everthing execpt required numbers while reading from .dat file?

I have

a .dat
file it contains data as:

count ------- 234 (1 row)

I want to retrieve the only 234 as integer and want to store in a variable in Shell script file.

I have used the below command:

WORKFLOW_STATUS_COUNT=`cat status_count.dat | tr -d " "`

but it is getting output as:

count ------- 234 (1 row)

but expected is 234 integer value.

Answer Source

You can use:

WORKFLOW_STATUS_COUNT=`cat status_count.dat | cut -d" " -f 3 `

cut splits the row with the spacebar delimeter and then choose only the third column

If the file come from Windows source you can try use dos2unix:

WORKFLOW_STATUS_COUNT=`cat status_count.dat | dos2unix | cut -d" " -f 3 `
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download