I have a few hundred files that each contain a single numeric entry (ex: 5243564). Each file name has an identifier (FT, FM, FA, etc) and a date (ex: FT.20180315.txt).

I would like to combine unique identifier files and their number entry into a single file AND bring the date with it. So a single FT file would look like:

20180315 5243564
20180316 1234566


This would do it: cat *FT* > FT.txt .

But that won't bring each files' date with it.


An awk one-liner should get the job done.

awk '{ printf("%s %s\n", substr(FILENAME, 4, 8), $0); }' FT.*.txt > FT.txt

I tested it on the examples you provided. Adjust as necessary.


Here is a simple script, that would do what you need. It is minimalistic, but shows the basic steps. Let's assume this script is called: combine.sh


#Add logic for better parsing of args.

# For each file in the Search Directory
# that starts with the Unique Identifier...
for f in "$SEARCHDIR/$IDENT"*; do

   datestr=$(echo "$f" | cut -d '.' -f2)
   value=$(cat "$f")
   echo "$datestr $value"

Then you could simply do:

$> combine.sh FT /path/to/my/datadir > FT.txt
  • Quote your variables: echo "$f", cat "$f" -- actually value=$(<"$f") is a bash builtin for $(cat "$f") – glenn jackman Mar 21 '18 at 19:49
  • @don_crissti - creates a for loop and assigns each filename in the output of ls to the variable f. – Dan K Mar 21 '18 at 20:01
  • @glennjackman - Good point about the variable quoting - thanks! I prefer to keep 'cat' command, as it's a little more readable. – Dan K Mar 21 '18 at 20:02
  • @don_crissti - no reason; that works too and is more readable. glennjackman made the edit in the solution above. – Dan K Mar 21 '18 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.