Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting Help 2

Status
Not open for further replies.

TekiePrincess

Programmer
Jun 1, 2009
18
US
I need to create a shell script to perform the following task:
1. Loop through a directory and ororead the file name of the .txt file and save it as a variable.
2. Read the line of that .txt file and from the first " to the last " save that information as a variable.
3. Other processes will be done then the file will be renamed to that of the second variable and saved to another directory.

Can someone please guide me in the right direction to get this done. I am very new to unix scripting and having a hard time with the variable portion of this task.
 
Hi

[ul]
[li]Please use descriptive subject.[/li]
[li]Post the script you wrote so far.[/li]
[li]For the Attachment field the explanation says "Type or paste full url (", so pasting you local file's path is useless. ( I mean, uploaded the file to a file hosting service then post its URL. )[/li]
[/ul]

Feherke.
 
1a) ororead is a typo, read is meant?
1b) 'the file name, the file' are singulars, so why do we have to loop?
1c) If it is just one file:
Code:
fnam=$(ls *.txt)

2) 'Read the line of that .txt file' - is there just one line?
Code:
fnam2=$(sed 's/.*"\(.*ß)".*/\1/' $fnam

3)
Code:
mv $fnam otherDir/$fnam2




don't visit my homepage:
 
stefanwagner
Thank you so much on helping to get this started to answer your questions:
1a) ororead is a typo, read is meant? Sorry, yes that was a typo. I did mean to say read.

1b) 'the file name, the file' are singulars, so why do we have to loop? I need to loop because there can be more than one file located in this directory at any given time.

2) 'Read the line of that .txt file' - is there just one line? No, there is more than one line in the text file but the information I need is located on the third line and this line will never change.
 
Hi

Code:
[b]for[/b] f [b]in[/b] [teal]*.[/teal]txt[teal];[/teal] [b]do[/b]
  [navy]l[/navy][teal]=[/teal][green][i]"$( sed -n '3{p;q}' "$f" )"[/i][/green]
  echo mv [green][i]"$f"[/i][/green] [green][i]"otherdir/$l"[/i][/green]
[b]done[/b]
Tested with [tt]bash[/tt] and [tt]mksh[/tt], using GNU [tt]sed[/tt].

Feherke.
 
Feherke,
Thanks this was very helpful.

I really appreciate the quick response from both you and stefanwagner. I will be sure to let you know how it turns out.


Again Thanks...
 
The suggestions I have received have so far has worked great up to the point where it is using the information on line three of the text file. This was my fault because I think I forgot to mention that this line contains more than just the filename. I have the following script so far:
#! /usr/bin/ksh
f=$(ls *.txt)
for f in *.txt; do
fn="${f%.*}"
l="$( sed -n '3p' "$f" )"
mv "${fn##*/}.dat" "Test/$l.dat"
done

The text file contains information similar to the following on line 3: UserFilename="dps_test_file.txt"

So my results end up being: UserFilename="dps_test_file.txt".dat as my filename.
So how can I just get my results to be dps_test_file.dat
 
The f=$(ls *.txt) line serves no purpose, you may as well remove it.

You should keep the sed script as Feherke suggested, i.e. 3{p;q;}, because this saves reading unnecessarily through the rest of the file (only important if they can be big files).

You can add more to the sed script to remove the parts you don't want, e.g.

Code:
l=$(sed -n '3{s/UserFilename="//;s/.txt"$//;p;q;}' "$f")



Annihilannic.
 
Final script...
Annihilannic,
Your suggestion was very helpful the only change I needed to make was I had to put a space after the number 3 and before I start to identify pattern string and this worked exactly as expected.

#! /usr/bin/ksh
for f in *.txt; do
fn="${f%.*}"
l=$(sed -n '3 {s/UserFilename="//;s/.txt"$//;p;q;}' "$f")
mv "${fn##*/}.dat" "Test/$l.dat"
done

Thanks you everyone for all of the help.
 
Hello,
I have been asked to expand the script that I have been working on to move the file to a specific directory based on whether the file names contains a particular string:
ie; PICK or LOCATOR or SHIP
PICK_XXXX_XXXX_XXX
LOCATOR_XXXX_XXXX_XXXX
SHIP_XXXX_XXXX_XXX

I started to try to modify my current script to accomodate this change and have come up with the following:

for f in *.txt; do
fn="${f%.*}"
l=$(sed -n '3 {s/UserFilename="//;s/.txt"$//;p;q;}' "$f")
echo $1
sed -n 's/*//:s/_$//;p;q;' "$l"
if [$l = 'locator']
then
mv "${fn##*/}.dat" "Locator/$l.dat"
elif [$l= 'pick']
then
mv "${fn##*/}.dat" "Pick/$l.dat"
elif [$l ='ship']
then
mv "${fn##*/}.dat" "Ship/$l.dat"
fi

Can someone please tell me whee I am going wrong with this.

Thanks.
 
Code:
echo $1

I presume you meant to echo $l there (i.e. the letter ell, not the digit one)?

Code:
sed -n 's/*//:s/_$//;p;q;' "$l"

I think you are attempting to strip off everything after the underscore here, is that right? With this code, sed will attempt to run those commands against the input file contained in $l, which at this point only contains a partial filename, so the file does not exist. If you want to use sed to modify a variable, you need to echo it to sed's standard input and specify no filename, something like this:

Code:
prefix=$( echo "$l" | sed 's/_.*//' )

In any case, I don't think this step is necessary, since you can use filename globbing type expressions in your if statements, e.g.

Code:
if [[ $l = LOCATOR_* ]]
then
    #...

Although in your case I'd use a case statement. Note too that the matching is case sensitive. I haven't tested this code.

Code:
for f in *.txt; do
   fn="${f%.*}" 
   l=$(sed -n '3 {s/UserFilename="//;s/.txt"$//;p;q;}' "$f")
   case $l in
      LOCATOR_*)  mv "${fn##*/}.dat" "Locator/$l.dat" ;;
      PICK_*)     mv "${fn##*/}.dat" "Pick/$l.dat"    ;;
      SHIP_*)     mv "${fn##*/}.dat" "Ship/$l.dat"    ;;
      *)          echo "error, unknown file type: $l" ;;
   esac
done

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top