find a string in a string using awk
Asked Answered
A

5

6

here is column 6 in a file:

ttttttttttt
tttttttttt
ttttttttt
tttttttattt
tttttttttt
ttttttttttt

how can I use awk to print out lines that include "a"

Anechoic answered 12/12, 2011 at 21:30 Comment(0)
B
7

If you only want to search the sixth column, use:

awk '$6 ~ /a/' file

If you want the whole line, any of these should work:

awk /a/ file

grep a file

sed '/^[^a]*$/d' file
Bretbretagne answered 12/12, 2011 at 21:34 Comment(1)
Your sed solution can be simplified to sed '/a/!d'Zamarripa
L
3

If you wish to print only those lines in which 6th column contains a then this would work -

awk '$6~/a/' file

Litigious answered 12/12, 2011 at 21:43 Comment(1)
bad comment deletedPaperback
A
2

if it is an exact match (which yours is not) you're looking for:

$6 == "a"

http://www.pement.org/awk/awk1line.txt is an excellent resource

awk can also tell you where the pattern is in the column:

awk '{++line_num}{ if ( match($6,"a")) { print "found a at position",RSTART, " line " ,line_num}  }' file

though this example will only show the first "a" in column 6; a for loop would be needed to show all instances (I think)

Ardine answered 13/2, 2015 at 4:42 Comment(0)
H
1

You could try

gawk '{ if ( $1 ~ /a/ ) { print $1  } }' filename
Hypermeter answered 12/12, 2011 at 21:39 Comment(1)
Could you elaborate your answer to show why it works?Phoenicia
W
0

It was relatively difficult to find good documentation easily on the web (my search didn't show the official one, I don't know why), but if you use the awk man in bash itself you will see all that you need (at least for awk).

Here you'll find everything about string manipulation, which is wonderful and VERY USEFUL!

Here You'll find everything about value comparison, numerical or otherwise. Which is also NECESSARY! (Locale can influence the result of logical expressions, be aware)

From docs:

Expression Result
x < y True if x is less than y
x <= y True if x is less than or equal to y
x > y True if x is greater than y
x >= y True if x is greater than or equal to y
x == y True if x is equal to y
x != y True if x is not equal to y
x ~ y True if the string x matches the regexp denoted by y
x !~ y True if the string x does not match the regexp denoted by y
subscript in array True if the array array has an element with the subscript subscript

The following list of expressions illustrates the kinds of comparisons awk performs, as well as what the result of each comparison is:

  1. 1.5 <= 2.0: Numeric comparison (true)
  2. "abc" >= "xyz": String comparison (false)
  3. 1.5 != " +2": String comparison (true)
  4. "1e2" < "3": String comparison (true)
  5. a = 2; b = "2" a == b: String comparison (true)
  6. a = 2; b = " +2" a == b: String comparison (false)

In this example:

echo 1e2 3 | awk '{ print ($1 < $2) ? "true" : "false" }' -| false

the result is ‘false’ because both $1 and $2 are user input. They are numeric strings—therefore both have the strnum attribute, dictating a numeric comparison. The purpose of the comparison rules and the use of numeric strings is to attempt to produce the behavior that is “least surprising,” while still “doing the right thing.”

String comparisons and regular expression comparisons are very different. For example:

x == "foo"

has the value one, or is true if the variable x is precisely ‘foo’. By contrast:

x ~ /foo/

has the value one if x contains ‘foo’, such as "Oh, what a fool am I!".

The righthand operand of the ‘~’ and ‘!~’ operators may be either a regexp constant (/.../) or an ordinary expression. In the latter case, the value of the expression as a string is used as a dynamic regexp (see How to Use Regular Expressions; also see Using Dynamic Regexps).

A constant regular expression in slashes by itself is also an expression. /regexp/ is an abbreviation for the following comparison expression:

$0 ~ /regexp/

One special place where /foo/ is not an abbreviation for ‘$0 ~ /foo/’ is when it is the righthand operand of ‘~’ or ‘!~’. See Using Regular Expression Constants, where this is discussed in more detail.

Hope it helps!

Windham answered 17/8 at 11:55 Comment(2)
A better place to post this answer, which is not an answer to the question asked here, is: https://mcmap.net/q/686211/-explain-awk-commandCarrasco
I tried to be as didactic as possible, covering every tool and its possibilities around what you asked. Sorry if I didn't please you, man, if necessary I'll remove the answer... I just want to help. ;DWindham

© 2022 - 2024 — McMap. All rights reserved.