Skip to main content
added 324 characters in body
Source Link
Stéphane Chazelas
  • 584.7k
  • 96
  • 1.1k
  • 1.7k

Awk is a command which is an interpreter for a programming language that is aimed at simple text processing. Though it can, it's not really meant to be used to call other commands like the head command.

Here, you could have a shell do the work:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec sh -c '
  for file do
    IFS= read -r line < "$file" &&
       case $line in
         ("#!"*) printf "%s\n" "$file: $line"
       esac
  done' sh {} +

OrIf you wanted to do it with awk, with GNU awk:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
    /^#!/ {print FILENAME ": " $0}; {nextfile}' {} +

With other awks:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
   BEGIN {
     for (i = 1; i < ARGC; i++)
       if ((getline < ARGV[i]) > 0 && /^#!/)
         print ARGV[i] ": " $0
     exit
   }' {} +
find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec sh -c '
  for file do
    IFS= read -r line < "$file" &&
       case $line in
         ("#!"*) printf "%s\n" "$file: $line"
       esac
  done' sh {} +

Or with GNU awk:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
    /^#!/ {print FILENAME ": " $0}; {nextfile}' {} +

With other awks:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
   BEGIN {
     for (i = 1; i < ARGC; i++)
       if ((getline < ARGV[i]) > 0 && /^#!/)
         print ARGV[i] ": " $0
     exit
   }' {} +

Awk is a command which is an interpreter for a programming language that is aimed at simple text processing. Though it can, it's not really meant to be used to call other commands like the head command.

Here, you could have a shell do the work:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec sh -c '
  for file do
    IFS= read -r line < "$file" &&
       case $line in
         ("#!"*) printf "%s\n" "$file: $line"
       esac
  done' sh {} +

If you wanted to do it with awk, with GNU awk:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
    /^#!/ {print FILENAME ": " $0}; {nextfile}' {} +

With other awks:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
   BEGIN {
     for (i = 1; i < ARGC; i++)
       if ((getline < ARGV[i]) > 0 && /^#!/)
         print ARGV[i] ": " $0
     exit
   }' {} +
added 324 characters in body
Source Link
Stéphane Chazelas
  • 584.7k
  • 96
  • 1.1k
  • 1.7k
find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec sh -c '
  for file do
    IFS= read -r line < "$file" &&
       case $line in
         ("#!"*) printf "%s\n" "$file: $line"
       esac
  done' sh {} +

Or with GNU awk:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
    /^#!/ {print FILENAME ": " $0}; {nextfile}' {} +

With other awks:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
   BEGIN {
     for (i = 1; i < ARGC; i++)
       if ((getline < ARGV[i]) > 0 && /^#!/)
         print ARGV[i] ": " $0
     exit
   }' {} +
find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec sh -c '
  for file do
    IFS= read -r line &&
       case $line in
         ("#!"*) printf "%s\n" "$file: $line"
       esac
  done' sh {} +

Or with GNU awk:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
    /^#!/ {print FILENAME ": " $0}; {nextfile}' {} +
find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec sh -c '
  for file do
    IFS= read -r line < "$file" &&
       case $line in
         ("#!"*) printf "%s\n" "$file: $line"
       esac
  done' sh {} +

Or with GNU awk:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
    /^#!/ {print FILENAME ": " $0}; {nextfile}' {} +

With other awks:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
   BEGIN {
     for (i = 1; i < ARGC; i++)
       if ((getline < ARGV[i]) > 0 && /^#!/)
         print ARGV[i] ": " $0
     exit
   }' {} +
Source Link
Stéphane Chazelas
  • 584.7k
  • 96
  • 1.1k
  • 1.7k

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec sh -c '
  for file do
    IFS= read -r line &&
       case $line in
         ("#!"*) printf "%s\n" "$file: $line"
       esac
  done' sh {} +

Or with GNU awk:

find . -type f \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' \
                  -o -name '*.html' \) -exec awk '
    /^#!/ {print FILENAME ": " $0}; {nextfile}' {} +