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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rename a file with content from the file itself 1

Status
Not open for further replies.

stfaprc

Programmer
Feb 10, 2005
216
US
I have a set of files with a line:
%%Title: C:\...tHold\dfA060TULIP1.1ADE
with "dfA060TULIP1" changing for each file

The filename is d001 , d0002, d003, etc.

How can I rename (or copy) the file d00* to the name after the word "Title:" in each file? I will not know what the name after "Title:" will be ahead of time.

thanks.
 
Hi

Try this :
Code:
for f in w*; do
  n="$( sed -n 's/^%%Title: \(.*\\\)\?\(.*\)/\2/p' "$f" )"
  [ "$n" ] && echo mv "$f" "$n"
done
If the output is as you wish, remove the [tt]echo[/tt] and run it again.

Note that if if you have input files with space in their name or you care about overwriting existing files, you will have to modify the above commands.

Feherke.
 
Sorry, but a pathname like C:\...tHold\dfA060TULIP1.1ADE is illegal for UNIX ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Actually,
C:\...tHold\dfA060TULIP1.1ADE
is a file name. Which *nix does not have a problem with.

However, windoz does have a problem with that name. So I would need to pick up the characters from the last "\" onword. By brute force I could figure this out with 'cut' but I'm sure that 'sed' would be able to do this (even though i've come up empty with searching "sed getting right most slash")
 
OOps, sorry, 'C:\...tHold\dfA060TULIP1.1ADE' is effectively legal for *nix.
pick up the characters from the last "\"
echo 'C:\...tHold\dfA060TULIP1.1ADE' | sed 's/.*\\//'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
feherke (Programmer)
6 Apr 09 10:27
Code:
for f in w*; do
  n="$( sed -n 's/^%%Title: \(.*\\\)\?\(.*\)/\2/p' "$f" )"
  [ "$n" ] && echo mv "$f" "$n"
done
HEY! this worked perfectly:
Code:
mv d36520-001 dfA017TULIP1.C8A
mv d36521-001 dfA018TULIP1.C8B
mv d36522-001 dfA019TULIP1.C8C
mv d36523-001 dfA020TULIP1.C8D
mv d36524-001 dfA021TULIP1.C8E
mv d36525-001 dfA022TULIP1.C8F
mv d36526-001 dfA023TULIP1.C90
mv d36527-001 dfA024TULIP1.C91
mv d36528-001 dfA025TULIP1.C92
mv d36529-001 dfA026TULIP1.C93
but I don't see how the sed is picking up just the characters after the last "\" ?
 
Hi

stfaprc said:
but I don't see how the sed is picking up just the characters after the last "\" ?
Code:
                               [COLOR=#800000 #FFC0C0]%%Title: [/color][COLOR=#008000 #C0FFC0]C:\...tHold\[/color][COLOR=#000080 #C0C0FF]dfA060TULIP1.1ADE[/color]
                            s/^[COLOR=#800000 #FFC0C0]%%Title: [/color][highlight #C0FFC0][gray]\[/gray][red]([/red][green].[/green][blue]*[/blue][gray]\[/gray][fuchsia]\[/fuchsia][gray]\[/gray][red])[/red][gray]\[/gray][teal]?[/teal][/highlight]  [COLOR=#000080 #C0C0FF]\(.*\)[/color]/[COLOR=#000080]\2[/color]/p
                                         [red]|[/red][green]|[/green][blue]|[/blue] [fuchsia]|[/fuchsia] [red]|[/red] [teal]|[/teal]
[red]start capture group ---------------------'[/red][green]|[/green][blue]|[/blue] [fuchsia]|[/fuchsia] [red]|[/red] [teal]|[/teal]
[green]any character ----------------------------'[/green][blue]|[/blue] [fuchsia]|[/fuchsia] [red]|[/red] [teal]|[/teal]
[blue]previous item ( the[/blue] [green].[/green] [blue]) 0 or more times ---'[/blue] [fuchsia]|[/fuchsia] [red]|[/red] [teal]|[/teal]
[fuchsia]backslash -----------------------------------'[/fuchsia] [red]|[/red] [teal]|[/teal]
[red]end capture group -----------------------------'[/red] [teal]|[/teal]
[teal]previous item ( the[/teal] [red]group[/red] [teal]) 0 or 1 times --------'[/teal]
Note that you probably can spare the question mark ( ? ) multiplier together with the grouping parenthesis ( () ). I added them in case there is a file name without path.

Feherke.
 
stfaprc, wasn't my suggested sed simpler ?
Code:
for f in d*; do
  n=$(sed -n '/^%%Title:/s/.*\\//p' $f)
  [ "$n" ] && mv $f "$n"
done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV, yes it seems simpler but you didn't post it :)

Anyway, new issue:
doing either
n="$( sed -n 's/^%%Title: \(.*\\\)\?\(.*\)/\2/p' "$f" )"
or
n=$(sed -n '/^%%Title:/s/.*\\//p' $f)

followed by:
[ "$n" ] && cp "$f" /var/spool/cups/ftp/"$n"

results in filenames of:
dfA061TULIP1.13B2? dfA122TULIP1.13EF? dfA183TULIP1.142C?

the "?" in actuallity being a "\r" , which I take it is from the end of the "%%Title" line. I thought 'chop' was a way to remove "\r" but either I was dealing with a system that had had 'chop' as an alias to something or I forgot that 'chop' is only for PERL.
So, how can I avoid the "\r" being appended to the filename?

 
but you didn't post it
Reread my post timestamped 6 Apr 09 15:41.

how can I avoid the "\r" being appended to the filename
Code:
for f in d*; do
  n=$(awk -F/ 'BEGIN{RS="\r\n"}/^%%Title:/{print $NF;exit}' $f)
  [ "$n" ] && mv $f "$n"
done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OOps, sorry for the typo.
Code:
for f in d*; do
  n=$(awk -F'\\' 'BEGIN{RS="\r\n"}/%%Title:/{print $NF;exit}' $f)
  [ "$n" ] && mv $f "$n"
done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
well I was close :)
i had tried
Code:
s/[\r]*$//

Thanks guys , that part worked.
Now another problem has popped up in scripting, I'll start a new thread for that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top