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

Using SED to make directories 2

Status
Not open for further replies.

jmarkus

Technical User
Oct 15, 2002
124
CA
I have a file: 12345-6789A.prt
I would like to create a directory: 12345-6789+A/

If I have 50 similarily named files (#####-####X.prt) I would like to create 50 directories.

I think I can use sed, for example:

for file in *;
do mkdir `echo $file | sed 's/'$1'/'$2'/g'` ;
done

where $1 is "12345-6789" and $2 is "12345-6789+", but I don't know how to make $1 and $2 be what I want.

Can someone help?

Thanks,
Jeff
 
Try that :

[tt]
for file in ?????-????X.prt
do
mkdir `echo $file | sed 's/\(.\)\.prt/+\1/'`
done
[/tt]


Jean Pierre.
 
Maybe something like this:
Code:
for f in [0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]?.prt
do
  mkdir `echo $f | sed 's!\(.\)!.prt/+\1/'`
done

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I'm not an expert in sed & awk but has been played around with it recently and came up with this solution.
#!/bin/sh
for f in *.prt
do
mkdir `echo $f | awk ' {
loc = index($1,".")
loc--
backend = substr($1, loc, 1)
loc--
frontend = substr($1, 0, loc)
printf("%s+%s", frontend, backend);
}'`
done

Enjoy,
dreamer
 
Here's a solution that doesn't use awk...
[tt]
for file in ?????-?????.prt
do
stem=${file%.prt}
mkdir ${stem%?}+${stem#?????-????}
done
[/tt]
 
Okay, I just came across a snag. I used:

for f in [0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]*.prt
do
mkdir `echo $f | sed 's!\(.\)!.prt/+\1/'`
done

but then realized that sometimes my file can be named 12345-6789X.prt and sometimes 12345-6789XYZ.prt. I always want to add the "+" after the 12345-6789. but this gives me 12345-6789X+YZ, when it should be 12345-6789+XYZ. How do I do that?

Thanks,
Jeff
 
Try something like this:
for f in [0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]*.prt
do
mkdir `echo $f | sed 's!\([A-Z]\)!+\1!;s!.prt$!!'`
done

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I think it's time for this man to be taught how to fish. I keep coming across variations of the part name that I'm not sure how to deal with (what about lower case, what if there is a letter followed by a number - eg 12345-6789A00.prt), etc.

Can you please break down the last bit of code and tell me what each part does?

Thanks,
Jeff
 
sed 's!\([A-Z]\)!+\1!;s!\.prt$!!'
1)s!
Substitute command
2)\([A-Z]\)!
Substitute what ? the 1st uppercase letter
The \( \) pair mean store it
3)+\1!
Substitute with what ? a "+" character followed by the 1st stored string (see point 2)
4);s!
New substitute command for the same input line
5)\.prt$!
Substitute what ? the string ".prt", the $ meaning this string should be at the end of the input line
6)!
Substitute with what ? nothing, ie an empty string
Anyway, man sed

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thanks PH!

I've tried to figure out from the man pages, but I find it is very cryptic and doesn't have alot of examples.

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top