• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

unix shell script: I need to split a file by line into variables: source, destination and file

I have a unix shell script (below) that will read a file of filenames and copy the file if found into a destination dirctory. I need to modify it to read a file that will have a source path, destination path and filename instead of just filename. Currently the source and destination are static. Please see example data below code. Can you help?

Shell script:

#!/bin/sh

source="/usr/nasas/bin/TEST/"
dest="/usr/nasas/bin/TEST/TEST2/"
yourfile="/usr/nasas/bin/TEST/testfile"

while read line
do
  file=`find "$source" -name "*$line*"`
  if [ -n "$file" ]
  then
    cp $file $dest
  else
    echo "$line" >> "notfound"
  fi
done < $yourfile
echo script test


Example file data:

eng.new/UFO/ufoxil2c/SYM/BAC/ipadBX8.1 /archive/davidk 20040817204251-0001 3 0
eng.new/UFO/ufoxil2c/xproj/ufoxil2c.ucf /archive/davidk 20040817204251-0001 3 0
eng.new/UFO/ufoxil2c/SYM/BAC/RAM45X8.1 /archive/davidk 20040817204251-0001 3 0

Thank you,
Karl
0
KHSmith
Asked:
KHSmith
  • 3
  • 2
  • 2
  • +2
1 Solution
 
stefan73Commented:
Hi KHSmith,
You could use cut or awk to split the input lines. Awk is probably better:

awk < input '{printf "%s%s %s%s\n" , $1,$3,$2,$3}' > output

Just combine the $... fields in a way which suits you.

Cheers!

Stefan
0
 
koppchaCommented:
you can do this change

when you read a line ..use set -- to split the line in to positional parameters(assuming you are delimiting the fields in file by space) for example if the line in the file is like this .You should follow some convention that first attribute will be filename and second will be source path and third will be destination path so on..
filename.txt /home/koppcha/EE /home/raj/EE
so when you use
set -- $line
tmpFile=$1
tmpSrcPath=$2
tmpDstPath=$3

Assuming the file pattern i have specified above you can change your script like this

#!/bin/sh
while read line
do
 set -- $line
 yourfile=$1
 source=$2
 dest=$3
  file=`find "$source" -name "*$line*"`
  if [ -n "$file" ]
  then
    cp $file $dest
  else
    echo "$line" >> "notfound"
  fi
done < $yourfile
echo script test



0
 
ahoffmannCommented:
could you please explain your example file, what is source, what is destination and what are the trailing numbers?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
KHSmithAuthor Commented:
The source are files on an optical library - the destination is a NAS server - the trailing numbers are not necessary (even I am not sure what they mean). We are given a long list of files to retrieve - their location and a destination. The rest is someone elses problem (SEP). I am trying koppcha's code. Thanks all!
0
 
KHSmithAuthor Commented:
When I run koppcha's script it does not find the file file1 in the following line given the above code:

/usr/nasas/bin/TEST/ /usr/nasas/bin/TEST/TEST2 file1
/usr/nasas/bin/TEST/ /usr/nasas/bin/TEST/TEST2 file2
/usr/nasas/bin/TEST/ /usr/nasas/bin/TEST/TEST2 file3

It reports they are all not found. Does the line search for file1 anywhere in the line string?

file=`find "$source" -name "*$line*"`
  if [ -n "$file" ]
  then
0
 
koppchaCommented:
ok because your file pattern is different did you make these changes

assuming your file pattern is thi
sourcepath destinationpath file

change from
yourfile=$1
 source=$2
 dest=$3

to
yourfile=$3
 source=$1
 dest=$2
0
 
koppchaCommented:
post your script
0
 
TintinCommented:
#!/bin/sh

error()
{
  echo "$*" >&2
  exit 1
}

[ $# -ne 3 ] && error "Usage: $0 [src] [dst] [file]"

source=$1
dest=$2
filelist=$3

[ -f $filelist ] || error "$filelist not found"
[ -d $dest ] || error "$dest not found"
[ -d $source ] || error "$source not found"

while read file
do
  if [ -f $source/$file ]
  then
     cp $source/$file $dest
  else
     echo "$source/$file not found"
  fi
done <$filelist
0
 
ahoffmannCommented:
hmm, still a magic description of your example ...
Guessing:
   your conf file contains something like:
eng.new/UFO/ufoxil2c/SYM/BAC/ipadBX8.1 /archive/davidk 20040817204251-0001 3 0

where the first word is the file and the second word is the destination directory (words seperated by white space)
then I'd do as following:

awk '{print "[ -f "$1" ] && cp /usr/nasas/bin/TEST/"$1" /usr/nasas/bin/TEST/TEST2/"$2"/"$1}' your-conf-file|sh

for testing simply omit the final  |sh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now