1

I have a .txt file with 20 lines, and would love to get the last two letters of each line. it equals AA in every line then print Good. if not, print Bad.

line11111111111111111 AA
line22222222222222222 AA
line33333333333333333 AA
..................... 
line20202020202020202 AA

This is GOOD.

===========================

line11111111111111111 AB
line22222222222222222 AC
line33333333333333333 WD
..................... 
line20202020202020202 ZZ

This is BAD.

Did this but needs improvement : sed 's/^.*\(.\{2\}\)/\1/'

anubhava
  • 761,203
  • 64
  • 569
  • 643
Br3x
  • 754
  • 4
  • 10
  • 21
  • 3
    Please show your attempts – anubhava Jul 17 '19 at 19:11
  • my attempt is using sed: sed 's/^.*\(.\{2\}\)/\1/' but doesn't seem to work for me – Br3x Jul 17 '19 at 19:13
  • Possible duplicate of [Accessing last x characters of a string in Bash](https://stackoverflow.com/q/19858600/608639), [A command to print only last 3 characters of a string](https://unix.stackexchange.com/q/163481), [Is there a cleaner way of getting the last N characters of every line?](https://stackoverflow.com/q/24427009/608639), [Get the last 4 characters of output from standard out](https://stackoverflow.com/q/9219964/608639) and friends. – jww Jul 17 '19 at 19:23

5 Answers5

2

based on your file layout

$ awk '$NF!="AA"{f=1; exit} END{print (f?"BAD":"GOOD")}' file

note that you don't have to check the rest after the first non "AA" token.

karakfa
  • 66,216
  • 7
  • 41
  • 56
1

You may use a single command awk:

awk 'substr($0, length()-1) != "AA"{exit 1}' file && echo "GOOD" || echo "BAD"

substr($0, length()-1) will extract last 2 characters of every line. awk command will exit with 1 if we don't fine AA in any line.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • can improve like this : awk ' $NF !="AA" {exit 1}' ad.txt && echo "good" || echo "bad" – yoga Jul 17 '19 at 19:59
  • @yoga: I initially thought about that but that is only possible when last 2 characters have a whitespace before that. OP's example shows that but it is not mentioned in question. – anubhava Jul 17 '19 at 20:03
  • 3
    Either that or jww is around and didn't like the question so all answers were downvoted regardless of technical correctness. – David C. Rankin Jul 17 '19 at 20:12
  • 1
    as per the example of the OP, this will work and tested it as well. BTW i did not down vote. – yoga Jul 17 '19 at 20:43
0

This script shoud work with awk. The name of the txt file for me is .test you can change it with your file name.

if [ "$(awk '{ print $2 }' .test | uniq)" = "AA" ]; then 
  echo "This is GOOD"; 
else echo "This is BAD";
fi

How it works: First, awk is used to get the second column by awk '{ print $2 }' and using uniq command we are taking unique entries from each line. If all the lines are AA uniq makes it only 1 line. At last we check whether this last product is only "AA" (1 line string with 2 As) or not.

pegasuspect
  • 991
  • 4
  • 15
0

Use a grep invert-match to identify lines not ending with "AA":

if egrep -q -v AA$ input.txt; then echo "bad"; else echo "good";fi
Jürgen Hötzel
  • 18,997
  • 3
  • 42
  • 58
-1

Solution with grep and wc:

if [ "`grep -v 'AA$' your-file-here | wc -l`" == "0" ] ; then echo 'GOOD' ; else echo 'BAD' ; fi

The grep command checks for all lines not ending with AA and wc counts how many lines.

  • I'm not your downvote, but I suspect your downvote is due to using `==` (a string comparison, which should properly be `=` for strings) instead of using `-eq` for a numeric comparison. Additionally, don't use *backticks* for *command substitution*, instead use `$(...)`. Your intent appears to have been `if [ $(grep -v 'AA$' file | wc -l) -eq 0 ]; then echo good; else echo bad; fi` which while valid spawns no less that 4-subshells. – David C. Rankin Jul 17 '19 at 20:08