Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Find command + replace the extension (.xxx) by *

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-20-2017
royinfo.alain royinfo.alain is offline
Registered User
 
Join Date: Aug 2016
Last Activity: 24 April 2017, 3:44 PM EDT
Posts: 16
Thanks: 17
Thanked 0 Times in 0 Posts
Find command + replace the extension (.xxx) by *

Hello,

I'm on HP Unix and in a Job, I tried to extract all files from a folder, and replace the extension (.xxxx) by '*' , remove duplicates and move the result in a file..

Example :
Folder has : ABC, CCC.txt, CCC.sf, CCC.sfd, DDD

I need to generate and output file with :

Code:
    ABC*
    CCC*
    DDD*

the commands that I did (See below) is working, but it increased the size of the logs because in my folder it has more than 600 000 files.. Is I can used another command to doing it or do an echo off somewhere and echo on after the execution.. thanks


Code:
[ -f $MAITUT/temp_file_A ] && rm $MAITUT/temp_file_A
[ -f $MAITUT/BCK_TMP_FILES_TO_DELETE ] && rm $MAITUT/BCK_DATA_FILES_TO_DELETE


find * -type f -mtime -365 | while read FileName
do
echo ${FileName%.*}'*' >> $MAITUT/temp_file_A
done

# Remove duplicates

sort -u $MAITUT/temp_file_A > $MAITUT/BCK_DATA_FILES_TO_DELETE

Example of the Info that i have in the logs :


Code:
...

+ read FileName
+ echo ARCAEDF*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCAMF*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCAMF*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCCC*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCCC*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCCD*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName
+ echo ARCCD*
+ 1>> /umaitdevapp/home/maitdev/tmp/temp_file_A
+ read FileName

...

Thanks a lot
Alain

Last edited by Corona688; 04-20-2017 at 05:01 PM..
Sponsored Links
    #2  
Old Unix and Linux 04-20-2017
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 August 2017, 6:53 PM EDT
Location: Saskatchewan
Posts: 22,327
Thanks: 1,115
Thanked 4,214 Times in 3,897 Posts
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
The Following User Says Thank You to Corona688 For This Useful Post:
royinfo.alain (04-21-2017)
Sponsored Links
    #3  
Old Unix and Linux 04-20-2017
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 August 2017, 6:53 PM EDT
Location: Saskatchewan
Posts: 22,327
Thanks: 1,115
Thanked 4,214 Times in 3,897 Posts
Someone left a set -x in your script by accident, causing it print every line of execution to stderr. Remove the set -x and it will stop doing that.
    #4  
Old Unix and Linux 04-21-2017
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 17 August 2017, 9:29 PM EDT
Location: Simplicity
Posts: 3,676
Thanks: 298
Thanked 1,229 Times in 1,110 Posts
... or the script is run with "sh -x"; the -x option also turns on the debug mode.

The following will run faster, and log less in debug mode

Code:
find * -type f -mtime -365 |
# replace a trailing .xxx with *
  sed 's/[.][^.]*$//' |
# Remove duplicates
  sort -u > $MAITUT/BCK_DATA_FILES_TO_DELETE

The pipe passes the output to the next command without a temp file.
The Following User Says Thank You to MadeInGermany For This Useful Post:
royinfo.alain (04-21-2017)
Sponsored Links
    #5  
Old Unix and Linux 04-21-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 17 August 2017, 2:39 PM EDT
Location: Aachen, Germany
Posts: 11,166
Thanks: 287
Thanked 3,441 Times in 3,170 Posts
The request seems to be to add a .* to every single file name, have it an extension or not. Try this small adaption to MadeInGermany's proposal:

Code:
find * -type f -mtime -365 | sed 's/[.][^.]*$//; s/$/.*/' | sort -u

The Following User Says Thank You to RudiC For This Useful Post:
royinfo.alain (04-21-2017)
Sponsored Links
    #6  
Old Unix and Linux 04-21-2017
royinfo.alain royinfo.alain is offline
Registered User
 
Join Date: Aug 2016
Last Activity: 24 April 2017, 3:44 PM EDT
Posts: 16
Thanks: 17
Thanked 0 Times in 0 Posts
Many thank to all of you, I used both and run very well & fast..


Code:
set +x
 find * -type f -mtime -365 | sed 's/[.][^.]*$//; s/$/*/' | sort -u > $MAITUT/FILES_TO_DELETE


Moderator's Comments:
Find command + replace the extension (.xxx) by * Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-22-2017 at 03:03 AM.. Reason: Changed ICODE to CODE tags.
Sponsored Links
    #7  
Old Unix and Linux 04-21-2017
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 17 August 2017, 9:29 PM EDT
Location: Simplicity
Posts: 3,676
Thanks: 298
Thanked 1,229 Times in 1,110 Posts
My previous post missed the replacement * in sed 's/[.][^.]*$/*/'.
But still this is different from the original while loop: it does not add the * when there is no .xxxx extension.

BTW your last post has ICODE tags instead of Code tags. The latter work much better for multi-line code blocks.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find and replace using sed command kmanivan82 Shell Programming and Scripting 3 02-20-2013 03:05 PM
Find command to exclude files with no extension meenavin UNIX for Dummies Questions & Answers 3 11-28-2011 04:39 AM
What is find and replace command in unix? rajkumar_g Shell Programming and Scripting 4 10-15-2010 05:48 PM
find and replace command prakash.gr Shell Programming and Scripting 1 12-07-2009 03:21 PM
find and replace command in one line using one command vasikaran UNIX for Dummies Questions & Answers 10 08-20-2008 10:40 AM



All times are GMT -4. The time now is 11:06 PM.