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.
I have tried your solution on both macOS and Ubuntu 22.04 and got:
That is because
for file in ./my_dironly return./my_dir. It does not return a list of files in that directory.I found the following works. Notice the wildcard:
Update
If you don't mind using awk, the following also works:
The
FNR == 1expression 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:
Update 3
Here is how to do it in bash: Use the
trcommand to strip the CRLF: