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/'

3

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This appears to be the most efficient of the lot.
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.

4 Comments

can improve like this : awk ' $NF !="AA" {exit 1}' ad.txt && echo "good" || echo "bad"
@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.
Either that or jww is around and didn't like the question so all answers were downvoted regardless of technical correctness.
as per the example of the OP, this will work and tested it as well. BTW i did not down vote.
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 

Comments

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.

6 Comments

Prints "This is bad" every time.
do you populate .test file?
yes i did. i think it's the awk command, i cant see where it extracts the last two characters of each line ?
awk splits lines by whitespace by default so using { print $2 } it extracts second column. Try cat .test | awk '{ print $2 }' and see
Also UUOc (Unnecessary Use Of cat) awk '{print $2}' .test | ... instead of cat .test | awk '{print $2}' .test | ... (any time you use cat, if you are not actually concatenating files, there is a good chance it is a UUOc)
|
-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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.