Search code examples
shellscriptingechocut

echo output not getting assigned to a variable in shell script


end=echo $FSDB_FILE_NAME | rev | cut -d'_' -f 2 |rev
begin=echo $FSDB_FILE_NAME | rev | cut -d'_' -f 3 |rev
echo $end
echo $begin

echo abc_11204.00_15713.00_.csv  | rev | cut -d'_' -f 2 |rev ---- This works

But echo $end is not printing anything

I even tried:

 set end=echo abc_11204.00_15713.00_.csv  | rev | cut -d'_' -f 2 |rev
 echo $end 

This prints empty

Please help me with this

Sample input : abc_123.00_345.00_xyz.csv
Output : end=345.00
         begin=123.00

Solution

  • Could you please try following. Easy approach with awk.

    start=$(echo "$input_variable" | awk  -F'_' '{print $2}')
    end=$(echo "$input_variable" | awk  -F'_' '{print $3}')
    

    When I print variable's values it will be as follows:

    echo "$start"
    123.00
    echo "$end"
    345.00