0

I have a directory containing files that start with a date.

for file in ./my_dir/*; do
    first_line=$(head -n 1 "$file")
    echo $file:$first_line
done

This prints as below, which is expected:

./my_dir/file.md:23/07/2023

When I switch the order of the $file and $first_line like this:

for file in ./my_dir/*; do
    first_line=$(head -n 1 "$file")
    echo $first_line:$file
done

The output becomes:

:./my_dir/file.md

The $first_line is completely ignored. My expectation was it to be like this:

23/07/2023:./my_dir/file.md

I've tried changing the delimiter and the first line of the files. Using ${first_line}:${file}, "${first_line}:${file}" didn't work too.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
doneforaiur
  • 1,308
  • 7
  • 14
  • 21

1 Answers1

1

I have tried your solution on both macOS and Ubuntu 22.04 and got:

head: error reading './my_dir': Is a directory

That is because for file in ./my_dir only return ./my_dir. It does not return a list of files in that directory.

I found the following works. Notice the wildcard:

for file in ./my_dir/*
do
        first_line=$(head -n 1 "$file")
        echo "$first_line:$file"
done

Update

If you don't mind using awk, the following also works:

awk 'FNR==1 {print $0 ":" FILENAME}' ./my_dir/*

The FNR == 1 expression says if the line is the first line in a file.

Update 2

Here is an awk command which strips the trailing CRLF, which should fix your problem:

awk 'FNR==1 {sub(/[\r\n]*$/, ""); print $0 ":" FILENAME}' ./my_dir/*

Update 3

Here is how to do it in bash: Use the tr command to strip the CRLF:

for file in ./my_dir/*
do
        first_line=$(head -n 1 "$file"| tr -d "\r\n")
        echo "$first_line:$file"
done
Hai Vu
  • 37,849
  • 11
  • 66
  • 93