[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2005-04-06
9
Medium Priority
?
546 Views
Last Modified: 2013-12-26
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
Comment
Question by:KHSmith
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 13719874
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
 
LVL 8

Accepted Solution

by:
koppcha earned 750 total points
ID: 13719890
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13720090
could you please explain your example file, what is source, what is destination and what are the trailing numbers?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:KHSmith
ID: 13720860
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
 

Author Comment

by:KHSmith
ID: 13721160
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13721253
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13721271
post your script
0
 
LVL 48

Expert Comment

by:Tintin
ID: 13721320
#!/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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13722069
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question