1

I have a *.csv file as bellow. What I want to do is converting all units to one unit (uA). Anyone can help to do this in c shell??

enter image description here

1

Here is my code, it now works fine to me.

# Add a space between result and unit
sed -e 's/mA/ mA/g' -e 's/uA/ uA/g' myfile.csv

#Convert
    awk -F" " '{\
    if ($4 == "mA"){\
        $3*=1000;\
        $4 = "uA";}\
    else\
        $3=$3;\
    }1' OFS=" " myfile.csv >> new_myfile.csv
  • FS and OFS default to space so you don't need to set that, and awk can handle this without the separate sed: awk '$3~/mA$/{ sub(/mA$/,"",$3); $3=($3*1000)"uA"} 1' – dave_thompson_085 Nov 15 '16 at 10:12
0

I'm not sure it's worth doing in csh, but here's a crappy "bash" solution:

cat foo.csv | while IFS='' read -r line; do
    multiplicand="$(
        echo "$line" | 
        perl -pe 's/^.*?([\.0-9]+)mA$/\1/g'
    )"
    echo "$line" | \
    perl -pe 's/^(.*?)'"$(
        echo "$multiplicand" | 
        perl -pe 's/\./\\./g'
    )"'mA$/${1}'"$(
        bc -l <<< "$multiplicand"*1000 2>/dev/null | 
        perl -pe 's/\.0+//g'
    )"'uA/g'
done
  • Thanks Jan Kyu! I have tried and also edited your code but it didn't work for me. I am trying a new code with 'awk' command and if-else statement , and now my work can be done. – lhphuc Nov 15 '16 at 6:35

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.