简体   繁体   English

if-conditionw在bash中带有“+”和“ - ”符号

[英]If-conditionw with “+” and “-” symbol in bash

I and new to bash scripting and have tab-delimted text file with following columns 我是新手来编写脚本,并使用以下列的制表符分隔文本文件

A234  +  -
B654  -  +
C630  +  +
D707  -  -

I would like to compare traverse every line of a file and compare second and third column to get an output like following in a text file 我想比较遍历文件的每一行,并比较第二和第三列,以获得像文本文件中的跟随输出

A234 Plus and Minus
B654 Minus and Plus
C630 Plus and Plus
D707 Minus and Minus

here is my bash script 这是我的bash脚本

#!/bin/bash
filename="$1"
while read line; 
do
g_symbol=$(echo $line | awk '{print $2}')
t_symbol=$(echo $line | awk '{print $3}')
if [[($g_symbol == "+") && ($t_symbol == "-")]]
then
echo $filename "PLus and Minus"
elif [[($g_symbol == "-") && ($t_symbol == "-")]]
then
echo $filename "Minus and Minus"
elif [[($g_symbol == "-") && ($t_symbol == "+")]]
then
echo $filename "Minus and Plus"
else
echo $filname "Plus and Plus"
fi
done < "$filename" 

But when I execute it, I get the following error 但是当我执行它时,我得到以下错误

[[(+: command not found
[[(-: command not found
[[(-: command not found
[[(+: command not found

Kindly guide me. 请指导我。

It is easier to do using awk: 使用awk更容易:

awk 'BEGIN{a["+"]="Plus"; a["-"]="Minus"} {print $1, a[$2], "and", a[$3]}' file

A234 Plus and Minus
B654 Minus and Plus
C630 Plus and Plus
D707 Minus and Minus

You don't need to run awk multiple times (or at all) to extract the fields from each line. 您不需要多次(或根本不)运行awk来从每一行中提取字段。 Your error, though, is because you did not put whitespace around the [[ and ]] . 但是,你的错误是因为你没有在[[]]周围放置空格。 Those are, despite containing nonalphabetic characters, keywords, not part of the if statement's syntax. 尽管包含非字母字符,但这些关键字不属于if语句的语法。

#!/bin/bash
filename="$1"
while IFS=$'\t' read first g_symbol t_symbol; 
do
  if [[ ($g_symbol == "+") && ($t_symbol == "-") ]]
  then
    echo $filename "PLus and Minus"
  elif [[ ($g_symbol == "-") && ($t_symbol == "-") ]]
  then
    echo $filename "Minus and Minus"
  elif [[ ($g_symbol == "-") && ($t_symbol == "+") ]]
  then
    echo $filename "Minus and Plus"
  else
    echo $filname "Plus and Plus"
  fi
done < "$filename" 

A case statement may be simpler: case陈述可能更简单:

#!/bin/bash
filename="$1"
while IFS=$'\t' read first g_symbol t_symbol; 
do
  case "$g_symbol $t_symbol" in
    "+ -") echo "$filename" "PLus and Minus" ;;
    "- -") echo "$filename" "Minus and Minus" ;;
    "- +") echo "$filename" "Minus and Plus" ;;
    "+ +") echo "$filname" "Plus and Plus" ;;
    *) echo "$filename" "Something else" ;;
  esac
done < "$filename" 

As mentioned in the comment, you'd better use sed or awk for this... 正如评论中所提到的,你最好使用sedawk ...

Here is an example using sed : 以下是使用sed的示例:

sed 's/\s*\([+-]\)\s*\([+-]\)/ \1 and \2/; # Add the ' and ' keyword and strip spaces
     s/\+/Plus/g;                          # Replace '+' by string 'Plus'
     s/-/Minus/g                           # Replace '-' by string 'Minus'
' file                     

Try this: 尝试这个:

#!/bin/bash
filename="$1"
while read line; 
do
FirstColomn=$(echo $line | awk '{print $1}')
g_symbol=$(echo $line | awk '{print $2}')
t_symbol=$(echo $line | awk '{print $3}')
OutPutFileName="Output.txt"
if [[( $g_symbol == "+" ) && ( $t_symbol == "-" )]]
then
echo $FirstColomn" PLus and Minus" >>$OutPutFileName 
elif [[( $g_symbol == "-" ) && ( $t_symbol == "-") ]]
then
echo  $FirstColomn " Minus and Minus"  >>$OutPutFileName 
elif [[( $g_symbol == "-" ) && ( $t_symbol == "+" )]]
then
echo  $FirstColomn " Minus and Plus"  >>$OutPutFileName 
else
echo  $FirstColomn " Plus and Plus"  >>$OutPutFileName 
fi
done < $filename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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