Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active August 6, 2024 08:19
Show Gist options
  • Save kjunichi/e2511aa23bee8c14b75142dff45cbce8 to your computer and use it in GitHub Desktop.
Save kjunichi/e2511aa23bee8c14b75142dff45cbce8 to your computer and use it in GitHub Desktop.

awkでの文字列検索

先頭は1始まりで、0が返されるのは見つからなかった時

$0は行全体

test.txt

login abc
ftp ip
hello

test.sh

#!/bin/sh

awk -v add_v="123" '{
    if (match($0,/login/) != 0) {
        printf("%s %s[%s]\n", $0, add_v, match($0,/login/));
    } else {
        printf("%s[%s]\n", $0, match($0,/login/));
    }
}' test.txt

# -vはawkで使う変数定義の模様
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment