I'm trying to write script or command that will go through all files in the current catalog and print only those file names that NOT contain lines starting with specific string only in the first line.
Tried this grep -i "echo" *
command to fetch all occurrences of the "echo" string but how add to it checking this first line and returning only file names?
CodePudding user response:
Using GNU grep:
grep -LD skip -d skip -i '^echo' *
-L
lists non matching files, -D skip
skips device, FIFO and socket files, -d skip
skips directories.
CodePudding user response:
This Shellcheck-clean code prints the names of all files in the current directory that do not have the string 'echo' in the first line:
#! /bin/bash -p
unwanted='echo'
for file in *; do
[[ -f $file ]] || continue
IFS= read -r first_line <"$file"
[[ $first_line == *"$unwanted"* ]] || printf '%s\n' "$file"
done