Greg
Greg

Reputation: 1842

how do I define a filename suffix using gawk

I am trying to retrieve files archived on Fairlight CMI floppy disks four decades ago. There are many disks each with up to 80 files with different file extensions so the process needs to be automated. I use grep to extract a list of filenames, like so :

    $ grep Sounddsk2.img ls -a1 > filenames

and gawk to identify different file types based on their affix. A short sample of filenames can look like this

    bass.co
    bass.ss
    bass.vc
    flute5.co
    flute5.ss
    flute5.vc
    guitar.co
    guitar.ss
    guitar.vc

gawk will be used to output names of files ending in .ss from filenames into a directory called ss (based on answers to my earlier question found here) (accepted answer still open and yes, I'm still learning)

    $ gawk '/^([a-zA-Z0-9])\.ss$/' filenames > ss

What I need to know is how to use gawk to make a directory named ss where I can save .ss files extracted from the disk image. These questions looked promising but the answers led nowhere.

In my second attempted gawk script, filenames is scanned for files ending in .ss and record separators (i.e. .) are counted. If tally is not zero, the script makes a directory named ss where extracted .ss files will be saved (eventually).

I have tried to detach the suffix from the file name by defining . as the record separator but this is probably not the right way to do it because the command line below does not work if ss also appears in the file name.

    $ gawk ‘BEGIN{RS=“[.]”; filetype= “ss”; tally=0}  index($1, “ss”){tally++} END{print tally; mkdir ss}’ filenames

And in the next version of the command below, gawk cannot read the test condition it needs to make the ss directory

    $ gawk ‘BEGIN{RS=“[.]”; filetype= “ss”; tally=0}  index($1, “ss”){tally++} END{if (tally >0) {mkdir ss}}’ filenames

So my questions are :

  1. how do I define the filename suffix ss using gawk ?
  2. how do I test the condition necessary to create a directory named ss ?

Note

What is being saved in these examples are file names not the actual files. Having a list of file names will be used to automate the following command line where the executable a will get each archivedfile from the floppy disk image to make a localfile, like so

    $ a Sounddsk2.img get archived-file local-file.

I hope this all makes sense. Any suggestions for improvement are most welcome. Thanks.

Upvotes: 1

Views: 100

Answers (1)

Ed Morton
Ed Morton

Reputation: 203522

awk is a tool to manipulate text. The tool to manipulate (create/destroy) files (and processes) is a shell. You're trying to use the wrong tool for your job.

It sounds like this is what you're trying to do (untested):

while IFS= read -r file; do
    sfx="${file##*.}"
    mkdir -p "$sfx" &&
    touch "$sfx/$file"
done < filenames

or if you really only want the .ss files then (again untested):

sfx='ss'
grep "\.${sfx}$" filenames |
xargs -n 1 -I {} sh -c 'mkdir -p '"$sfx"' && touch '"$sfx"'/{}'

If neither of those is what you're trying to do then please edit your question to clarify your requirements.

Upvotes: 3

Related Questions