bash

If conditions

Ref: https://www.tldp.org/LDP/abs/html/comparison-ops.html

#!/bin/bash

a=4
b=5

#  Here "a" and "b" can be treated either as integers or strings.
#  There is some blurring between the arithmetic and string comparisons,
#+ since Bash variables are not strongly typed.

#  Bash permits integer operations and comparisons on variables
#+ whose value consists of all-integer characters.
#  Caution advised, however.

echo

if [ "$a" -ne "$b" ]
then
  echo "$a is not equal to $b"
  echo "(arithmetic comparison)"
fi

echo

if [ "$a" != "$b" ]
then
  echo "$a is not equal to $b."
  echo "(string comparison)"
  #     "4"  != "5"
  # ASCII 52 != ASCII 53
fi

# In this particular instance, both "-ne" and "!=" work.

echo

exit 0

To replace a string pattern with empty string in a file

files contains the following strings
  pic_000_1549487577_generic_combo_0_crop_001.jpg   green    [good: 0.01,  green: 0.99]
  pic_000_1549487577_generic_combo_0_crop_002.jpg   green    [good: 0.0,  green: 1.0]
  pic_000_1549487577_generic_combo_0_crop_003.jpg   green    [good: 0.01,  green: 0.99]

To replace a string pattern with empty string in a file

%s/_crop[0-9]*//g

output:
  pic_000_1549487577_generic_combo_0.jpg   green    [good: 0.01,  green: 0.99]
  pic_000_1549487577_generic_combo_0.jpg   green    [good: 0.0,  green: 1.0]
  pic_000_1549487577_generic_combo_0.jpg   green    [good: 0.01,  green: 0.99]


To replace a string from where it matches till the end
%s/\[.*//g

output:
  pic_000_1549487577_generic_combo_0.jpg   green
  pic_000_1549487577_generic_combo_0.jpg   green
  pic_000_1549487577_generic_combo_0.jpg   green

AWK

# awk using multiple delimiters.
example: space and ':'

awk -F'[ :]' '{print $6}' AIreport_classification_combo_labEq_notresized_argmax_count.txt

Last updated

Was this helpful?