1

I have database for persons and path to their files which stored on a computer. This data saved as (a very long) delimited text file as follows:

============
NAME: John
TIME: 12:31:25
PATH: ./a/b/v/c   #to some data related to the person
============
NAME: Jo
TIME: 11:21:11
PATH: ./c/s/d/f   #to some data related to the person 
============
NAME: Je
TIME: 10:51:15
PATH: ./q/n/h/l   #to some data related to the person
============

......  # very long list of entries in this format.

I want to create folder has the NAME of every entry and then copy all the stuff in his PATH to an OUTPUT folder. I used grep in the code bellow to include the PATH for every person in the command cp then to copy all the stuff for all the NAMES at once?

#!/bin/bash -f
source=PATH TO THIS TEXT DELIMITED FILE (log.log)
grep -o 'NAME.*' ./log.log | cut -f2- -d':'> ./name.log; 
grep -o 'PATH.*' ./log.log | cut -f2- -d':'> ./path.log; 
for i in $(cat ./name.log);do
mkdir ./${i};
    for ii in $(cat ./path.log); do 
(
      cp ${ii}/*.* ./${i}
done
)&
done

Due to my shallow experience in coding I think I complicated this code. I need suggestions to simplify it?

1 Answer 1

4

I might try something like this...

First, use awk to dynamically generate a shell script:

awk '
  /NAME:/ {
    name=gensub(/\"/, "", "g", substr($0, 7))
  }
  /PATH:/ {path=$2}
  name && /=====/ {
    printf "mkdir \"%s\"\n", name
    printf "rsync -a \"%s/\" \"%s/\"\n", path, name
  }
'

Given your input, this would generate:

rsync -a "./a/b/v/c/" "John/"
rsync -a "./c/s/d/f/" "Jo/"
rsync -a "./q/n/h/l/" "Je/"

Then run that shell script. You can combine this in a single operation:

awk '
  /NAME:/ {
    name=gensub(/\"/, "", "g", substr($0, 7))
  }
  /PATH:/ {path=$2}
  name && /=====/ {
    printf "mkdir \"%s\"\n", name
    printf "rsync -a \"%s/\" \"%s/\"\n", path, name
  }
'

I'm using rsync here for copying things, but of course you could just use cp -r.

Also, I notice that in your code you're using *.*. That's probably not what you want, unless you mean, "only copy files that contain a dot somewhere in the filename".

Update

I've modified this so it works correctly given:

============
NAME: "John Frederick"
TIME: 12:31:25
PATH: ./a/b/v/c   #to some data related to the person
============
6
  • @Iarsaks. You are great. kindly I got this error : Unmatched '
    – user88036
    Commented Aug 8, 2015 at 1:51
  • There's nothing that should be generating that error. Here is a terminal session in which I run the above awk script against your data. Are you able to run just the awk script (that is, do you only get the error when piping the output to | sh)?
    – larsks
    Commented Aug 8, 2015 at 1:54
  • Exactly! when I piping the output to |sh
    – user88036
    Commented Aug 8, 2015 at 1:57
  • 1
    Are all of your names single words? Are there quotation marks in the data?
    – larsks
    Commented Aug 8, 2015 at 1:58
  • Yes there are quotation marks for some of it. The names are single word and some are two words
    – user88036
    Commented Aug 8, 2015 at 2:07

You must log in to answer this question.