简体   繁体   中英

Bash script to manipulate two decimal numbers in a line

Two of the lines of data that I am processing look like this.

18 xy Pqr  -3879.65 xp9  a-kxp   Kap 97868.08 P8A jrh-uyjf iu-re
A4-18 usU Aqr 974.59  xpab9  Tb7k-p   ptx 1533.93  K-doe Uap-qe1

Main characteristics:

  • Every line has two decimal numbers.
  • The first number can be positive or negative but the second number is always positive.

I want to flip the sign of the first number (positive to negative and vice versa) and delete the second number.

With limited skills in bash, I wrote the following script in a brute force method. It looks so inelegant! Thank you for any pointers the forum members could possibly provide to make it better.

#replace whitespaces with "_" for easy 'sed'-ing
a=`echo "$this_line" | sed -e "s/ /_/g"`

#get head part with first decimal number
b=`echo $a | grep -Po '^.*?[-]?[0-9]+[\.][0-9]+'`

#pick the decimal number from the head part
c=`echo $b | grep -Po '[-]?[0-9]+[\.][0-9]+'`

#flip the sign of the decimal number from the head part
d=`echo -1 \* $c | bc -l`

#delete decimail number from the head part
e=`echo "$b" | sed -e "s/$c$//"`

#put back head part with the decimal number sign flipped
f=`echo $e$d`

#get tail part with second decimal number
g=`echo "$a" | sed -e "s/^$b//"`

#pick the decimal number from the tail part
h=`echo $g | grep -Po '^.*?[-]?[0-9]+[\.][0-9]+'`

#delete decimail number from the tail part
i=`echo "$g" | sed -e "s/^$h//"`

#join back without second decimal number and first decimal sign flipped
j=`echo $f"  "$i`

#replace back "_" by whitespace
modified_line=`echo "$j" | sed -e "s/_/ /g"`

What about something like

awk '
{
    flipped=0
    for (i=1; i< NF; i++) {
        if ($i ~ /-*[0-9]+\.[0-9]+/) {
            $i = (!flipped++) ? -$i : "";
        }
    }
    print
}
'

which produces

18 xy Pqr 3879.65 xp9 a-kxp Kap  P8A jrh-uyjf iu-re
A4-18 usU Aqr -974.59 xpab9 Tb7k-p ptx  K-doe Uap-qe1

在处理浮点数时,awk 是一个更好的工具,因为 Bash 没有浮点类型:

awk '{ printf ("%s %s %s %f %s %s %s %s %s\n", $1, $2, $3, -$4, $5, $6, $7, $9, $10) }' input_file
str="18 xy Pqr  -3879.65 xp9  a-kxp   Kap 97868.08 P8A jrh-uyjf iu-re"
echo "$str" \
  | grep -Eo -- '-?[0-9]*\.[0-9]+' \
  | head -n 1 \
  | awk '{print ($1 * -1)}'

Explanation

  • grep -Eo -- '-?[0-9]*\\.[0-9]+'' Will find any string that starts with optionally a hyphen followed by digits followed by a period followed by digits
  • head -n 1 will then retrieve only the First result from the grep
  • awk '{print ($1 *- 1)} will then print the first number times -1 (thereby flipping the sign)

oneliner

echo "$str" | grep -Eo -- '-?[0-9]*\.[0-9]+' | head -n 1 | awk '{print ($1 * -1)}'

A simpler and more readable solution using awk and variable substitution

#get the string
string="A4-18 usU Aqr 974.59  xpab9  Tb7k-p   ptx 1533.93  K-doe Uap-qe1"
#get the first number
firstNumber=$(echo $string | awk '{print $4}')
#get the second number
secondNumber=$(echo $string | awk '{print $8}')
#calculate absolute value
absoluteValue=${firstNumber#-}
#replace string
echo $string | sed s/$firstNumber/$absoluteValue/ | sed s/$secondNumber//

#!/bin/bash

msg="18 xy Pqr K -261.90 xp9 P a-kxp 7873.57 Kap P8A jrh-uyjf"

printf '%s\\n' "$msg"

#to preserve whitespaces, replace them with "_"

a= echo "$msg" | sed -e "s/ /_/g" echo "$msg" | sed -e "s/ /_/g"

#display the string to show breaking points

b= echo $a | sed -n "s/\\(^.*\\.[0-9].*_\\)\\([0-9]*\\.[0-9][0-9]\\)\\(.*$\\)/\\1|\\2|\\3/p" echo $a | sed -n "s/\\(^.*\\.[0-9].*_\\)\\([0-9]*\\.[0-9][0-9]\\)\\(.*$\\)/\\1|\\2|\\3/p"

echo $b

#delete the second decimal number

b= echo $a | sed -n "s/\\(^.*\\.[0-9].*_\\)\\([0-9]*\\.[0-9][0-9]\\)\\(.*$\\)/\\1 \\3/p" echo $a | sed -n "s/\\(^.*\\.[0-9].*_\\)\\([0-9]*\\.[0-9][0-9]\\)\\(.*$\\)/\\1 \\3/p"

#echo $b

#pick the only decimal number present in the remaining string

c= echo $b | grep -Po '[-]?[0-9]+\\.[0-9]+' echo $b | grep -Po '[-]?[0-9]+\\.[0-9]+'

#change sign of the decinal number

d= echo -1 \\* $c | bc -l echo -1 \\* $c | bc -l

#substitute the decinal number back to the string with new sign

e= echo ${b/$c/$d}

#replace back "_" by whitespace

modified_line= echo "$e" | sed -e "s/_/ /g" echo "$e" | sed -e "s/_/ /g"

printf '%s\\n' "$modified_line"

exit 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM