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

substitute file name 2

Status
Not open for further replies.

pearlofperls

Technical User
Apr 5, 2007
24
US
Hi

Im stuck in the following issue:

I have a directory where correct file names are:

*_0.txt
*_1.txt

Then through a publisher some incorrect file names are produced:

*_12.txt
*_0123.txt
*_04321.txt

all files that are incorrect above need to be replaced with ending in *_1.txt

therefore need to create a loop to find the wrong files in a dir ->that dont end in _1.txt or _0.txt and then modify the end of the files names to *_1.txt while retaning the orginal part of the filenames.


 
Hi

Try this, if the output is what you want, remove the [tt]echo[/tt] :
Code:
[b]for[/b] f [b]in[/b] [teal]*[^[/teal]_[teal]][^[/teal][purple]01[/purple][teal]].[/teal]txt[teal];[/teal] [b]do[/b]
  echo mv [green][i]"$f"[/i][/green] [green][i]"${f/_*./_1.}"[/i][/green]
[b]done[/b]
Tested with [tt]bash[/tt].

Feherke.
 
A more portable way:
Code:
ls *_*.txt | awk '!/_[01]\.txt$/{f=$0;sub(/_.*\./,"_1.",f);printf "mv \"%s\" \"%s\"\n",$0,f}' | sh

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
im runnning /usr/bin/ksh
I ran both suggestions..both errored out...

for fehreke i got the following error after i removed the echo:

"${f/_*./_1.}": bad substitution

for PHV i got the following error:

awk: syntax error near line 1
awk: illegal statement near line 1
 
Try nawk instead.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
for i in `ls *_??*.txt`;do mv $i ${i%_*}_1.txt;done

... in bash
 
ok woops, once again i should read the whole thread before opening dropping something random.

since its in ksh couldnt you just do something simple like this:

Code:
for file in `ls *_??*.txt`;do
   new=`echo $file|awk -F_ '{print $1}'`
   echo $file $new_1.txt
done

not exactly elegant but it would work...
 
exsnafu,

thanks for the input, however there are 2 issues occuring when running this.

1) The echo shows the files would be replaced by (blank).txt

2) It is trying to replace files that are *_0.txt which should not be happening. Per the orginal description those files are ok.

Result set:

11237339_28660819_6142.txt .txt
11237341_28660834_6144.txt .txt
11237342_28660849_6146.txt .txt
11237343_28660864_6148.txt .txt
12116271_38788311_34867.txt .txt
12116272_38788329_34869.txt .txt
12468556_43837378_0.txt .txt
12468556_43837378_9573.txt .txt
12468572_43837584_0.txt .txt
12468572_43837584_9602.txt .txt
12468586_43837803_0.txt .txt
12468586_43837803_9632.txt .txt
12468600_43838010_0.txt .txt
12468600_43838010_9663.txt .txt
12468615_43838209_0.txt .txt
12468615_43838209_9694.txt .txt
12468626_43838375_0.txt .txt
12468626_43838375_9721.txt .txt
12468650_43838744_0.txt .txt
12468650_43838744_9775.txt .txt
12468676_43839083_0.txt .txt
12468676_43839083_9827.txt .txt
12468686_43839270_0.txt .txt
12468686_43839270_9854.txt .txt
12468697_43839440_0.txt .txt
12468697_43839440_9879.txt .txt
12468725_43839815_0.txt .txt
12468725_43839815_9936.txt .txt
12468733_43839962_0.txt .txt
12468733_43839962_9956.txt .txt
12468744_43840112_0.txt .txt
12468744_43840112_9978.txt .txt
12468758_43840324_0.txt .txt
12468758_43840324_10009.txt .txt
12468771_43840530_0.txt .txt
12468771_43840530_10041.txt .txt
12468782_43840707_0.txt .txt
12468782_43840707_10069.txt .txt
12468795_43840899_0.txt .txt
12468795_43840899_10099.txt .txt
12468805_43841073_0.txt .txt
12468805_43841073_10126.txt .txt
12568892_44719691_14928.txt .txt
12568893_44719709_14930.txt .txt
12988571_51199617_0.txt .txt
12988571_51199617_30967.txt .txt
12988573_51199636_0.txt .txt
12988573_51199636_30969.txt .txt
14129227_69039012_0.txt .txt
14129227_69039012_89479.txt .txt
14129228_69039030_0.txt .txt
14129228_69039030_89481.txt .txt
14129229_69039048_0.txt .txt
14129229_69039048_89483.txt .txt
14129230_69039066_0.txt .txt
14129230_69039066_89485.txt .txt
14129231_69039084_0.txt .txt
14129231_69039084_89487.txt .txt
14129232_69039102_0.txt .txt
14129232_69039102_89489.txt .txt
7855722_17821829_4220.txt .txt
7855723_17821840_4222.txt .txt
7855725_17821851_4224.txt .txt
7855726_17821862_4226.txt .txt
7855727_17821873_4228.txt .txt
7855728_17821884_4230.txt .txt
7855729_17821895_4232.txt .txt
7855731_17821906_4234.txt .txt
7932640_18109833_0.txt .txt
8136525_18801966_0.txt .txt
8139702_18802356_6.txt .txt
8683101_20625003_3249.txt .txt
8683102_20625017_3251.txt .txt
8683103_20625032_3253.txt .txt
8683105_20625048_3255.txt .txt
8683106_20625074_3257.txt .txt
8683107_20625094_3259.txt .txt
8781215_20963120_22024.txt .txt
8781219_20963134_22026.txt .txt
8781221_20963154_22028.txt .txt
8781224_20963167_22030.txt .txt
 
So, as you don't seem to have space in the filenames:
Code:
ls *_*.txt | nawk '!/_[01]\.txt$/{f=$1;sub(/_.*\./,"_1.",f);printf "mv %s %s\n",$1,f}' | sh

53 files processed with your result set posted 26 Jun 09 12:47.
BTW, if you still get some error then, please, copy/paste the EXACT code you tried and say us on which flavor of *nix you tested our suggestions.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
im running a sh script on Sun OS

SunOS 5.10 Generic_118833-36 sun4u sparc SUNW,Sun-Fire-V490

here is the contents of script:

cd dir

ls *_*.txt | nawk '!/_[01]\.txt$/{f=$1;sub(/_.*\./,"_1.",f);printf "mv %s %s\n",$1,f}' | sh

exit 1


result set of script fix_awk.sh:

$ fix_awk.sh
awk: syntax error near line 1
awk: illegal statement near line 1
 
You may try the xpg4 version of awk.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
exsnafu,

i solved the part 2 of the problem of script where it was also trying to change 0.txt files as well. I changed the search criteria

for file in `ls *_*_!(0).txt';
do
new=`echo $file|awk -F_ '{print $1}'`
echo $file $new_1.txt
done

now the 1st issue still remains, the new files are not being created. The script echos files with blank spaces and ending with xml
I think the issue is with the new variable

The file criteria have two underscores and awk is only using one underscore and I think it gets confused.

What do you think?
 
pearlofperls,
As you don't seem to have a decent awk version on you box, here another (corrected) try:
Code:
ls *_*.txt | nawk '$1!~"_[01]\.txt$"{f=$1;sub("_[^_]*\.","_1.",f);printf "mv %s %s\n",$1,f}' | sh

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
pearl, oh.. oops.

this seems to work:
Code:
for file in `ls *_*_!(0).txt`; do
        new=${file%_*}
        mv $file $new"_1.txt"
done

[87][ap-03]:/home/jeff/tmp/test> ls -l *txt
-rw-r--r-- 1 jeff uucp 0 Jun 26 14:05 12468586_43837803_1.txt
-rw-r--r-- 1 jeff uucp 0 Jun 26 14:04 12468733_43839962_0.txt
-rw-r--r-- 1 jeff uucp 0 Jun 26 14:03 12468744_43840112_9978.txt
-rw-r--r-- 1 jeff uucp 0 Jun 26 14:03 12468782_43840707_0.txt
-rw-r--r-- 1 jeff uucp 0 Jun 26 14:04 12468782_43840707_10069.txt
[88][ap-03]:/home/jeff/tmp/test> ksh ./hmm
mv: 0653-405 12468586_43837803_1.txt and 12468586_43837803_1.txt are identical.
[89][ap-03]:/home/jeff/tmp/test> ls -l *txt
-rw-r--r-- 1 jeff uucp 0 Jun 26 14:05 12468586_43837803_1.txt
-rw-r--r-- 1 jeff uucp 0 Jun 26 14:04 12468733_43839962_0.txt
-rw-r--r-- 1 jeff uucp 0 Jun 26 14:03 12468744_43840112_1.txt
-rw-r--r-- 1 jeff uucp 0 Jun 26 14:03 12468782_43840707_0.txt
-rw-r--r-- 1 jeff uucp 0 Jun 26 14:04 12468782_43840707_1.txt



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top