Create directory and sub-directory with awk and bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create directory and sub-directory with awk and bash
Prev   Next
# 3  
Old 04-19-2019
I'm not sure if I completely follow, but I modified the script to utilize awk better. Is the below closer or what do you suggest? I included comments in the code as well. Thank you very much Smilie.

awk
Code:
DIR=/home/cmccabe/pre  ## define data directory
awk '
    # create an associative array (key/value pairs) based on the f1
    NR==FNR { for(i=2; i<NF; i+=2) a[substr($i,1,7)] = $NF; next } ## store the 7 digits in $2 in a

    # retrieve the first 7-char of each line in f2 as the key to test against the above hash
    { b = substr($2, 1, 7) } ## store the 7 digits in $2 in b

    # if find b, then print
    b in a { print a[b] "\t" $3 && $4 "\t" p }

    # save prev line to 'p'(these are the paths)
    { p = $0  } 

' RS= f1 RS='\n' f2 | for RDIR in "$DIR"/R_2019* ; do  ## # matching "R_2019*" to operate on desired directory and expand
                       TRIMSTR=${RDIR%%-x0.0*}  ## trim folder match in RDIR from -x0.0 and store in TRIMSTR
                         mv "$RDIR" "${TRIMSTR}"  ## trim folder
                           mkdir -p "$TRIMSTR/$a[b]/$p"  ## make folder/sub-folder/path
                       done ## end loop

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to check directory and create missing folder from file

In the below bash I am trying to ensure that all folders (represented by $folders) in a given directory are created. In the file f1 the trimmed folder will be there somewhere (will be multiple trimmed folders). When that trimmed folder is found (represented by $S5) the the contents of $2 printed... (19 Replies)
Discussion started by: cmccabe
19 Replies

2. Shell Programming and Scripting

awk to create link, download, and extract in sub-directory

The awk below will create sub-directories in a directory (which is always the last line of file1, each block separated by an empty line), if the number in line 2 (always the first 6 digits in the format xx-xxxx) of file2 is found in $2 of file1. This is the current awk output. If there is a... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

4. Shell Programming and Scripting

Create automated scan of specific directory using bash

I am trying to use bash to automate the scan of a specific directory using clamav. Having this in place is a network requirement. The below is an attempt to: 1. count the extensions (.txt, .jpeg) in a directory and write them to a virus-scan.log (section in bold) 2. scan each folder in the... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

How Create new directory and move files to that directory.?

Hi All, We have main directory called "head" under this we have several sub directories and under these directories we have sub directories. My requirement is I have to find the SQL files which are having the string "procedure" under "head" directory and sub directories as well. And create... (14 Replies)
Discussion started by: ROCK_PLSQL
14 Replies

6. Shell Programming and Scripting

Bash to create new directory by date followed by identifier and additional subdirectories

I have a bash that downloads a list and if that list has data in it then a new main directory is created (with the date) with several subdirectories (example1, example2, example3). My question is in that list there are portion of specific file types (.vcf.gz) - identifier towards the end that have... (0 Replies)
Discussion started by: cmccabe
0 Replies

7. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

8. Homework & Coursework Questions

Create script to add user and create directory

first off let me introduce myself. My name is Eric and I am new to linux, I am taking an advanced linux administration class and we are tasked with creating a script to add new users that anyone can run, has to check for the existence of a directory. if the directory does not exist then it has... (12 Replies)
Discussion started by: pbhound
12 Replies

9. Shell Programming and Scripting

Use awk to create new folder in current directory

Alright, I am sure this is a laughable question, but I don't know so I am going to ask anyway. I have a little script I am writing to take information from one source, recode it in a certain way, and print to files for each subject I have data for. This all works perfectly. I just want to put... (6 Replies)
Discussion started by: ccox85
6 Replies

10. UNIX for Advanced & Expert Users

unable to create any directory that uses numbers as the directory name

Version-Release number of selected component (if applicable): ================================================================== root@server # cat /etc/redhat-release Red Hat Enterprise Linux ES release 4 (Nahant Update 5) root@server # uname -a Linux server.integrityserver.net... (16 Replies)
Discussion started by: fed.linuxgossip
16 Replies
Login or Register to Ask a Question
create replica(1m)														create replica(1m)

NAME
create replica - Creates a replica of an existing directory in the specified clearinghouse SYNOPSIS
cdscp create replica directory-name clearinghouse clearinghouse-name ARGUMENTS
The full name of the directory. The full name of the clearinghouse in which you want to create the replica. DESCRIPTION
The create replica command creates a replica of an existing directory in the specified clearinghouse. Privilege Required You must have administer permission to the directory you intend to replicate and write permission to the clearinghouse that stores the new replica. The server principal needs read, write, and administer permission to the directory you intend to replicate. NOTES
This command is usually executed only by the network configuration procedure. To ensure that all replicas are consistent, perform an imme- diate skulk of the parent directory after issuing this command. This command is replaced at Revision 1.1 by the dcecp command and may not be provided in future releases of DCE. EXAMPLE
The following command creates a replica of the /.:/mfg directory in the clearinghouse /.:/Paris_CH. cdscp> create replica /.:/mfg clear- inghouse /.:/Paris1_CH RELATED INFORMATION
Commands: delete replica(1m), show replica(1m) create replica(1m)