Replace file name on certain "changeable string"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace file name on certain "changeable string"
# 1  
Old 11-15-2011
Replace file name on certain "changeable string"

Dear all,

I can use the following script to remove certain strings of my filename.
Code:
for filename in `find . -name *blah*`;
do
  new_name=`echo $filename | sed -e "s/blah//g" $filename`
  mv $filename $new_name
done


What if i had file names below and i wish to sed only certain part of the filename?


oringinal filename:
Code:
20111111_DATA_ABC_ABC_DEF_END1.bin
20111111_DATA_BCD_EDF_AAS_END2.bin

final filename:
Code:
20111111_DATA__END1.bin
20111111_DATA__END2.bin

Thanks and looking forward to hear from you.

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-16-2011 at 03:46 AM.. Reason: Please use code tags
# 2  
Old 11-15-2011
This is the way that I'd do it:


Code:
  find . -name "*stuff* | awk '
    {
        n = split( $1, a, "_" );
        printf( "mv %s %s_%s__%s\n", $1, a[1], a[2], a[n] );
    }
  ' # ksh    ### remove the comment before ksh after you verify that the commands generated are what you want.

This will generate the move commands. After you verify that they are correct, you can pipe the output directly to ksh or bash
# 3  
Old 11-15-2011
Code:
echo 20111111_DATA_ABC_ABC_DEF_END1.bin |awk '{print $1,$2,$NF}' FS=_ OFS=_

20111111_DATA_END1.bin

# 4  
Old 11-15-2011
Hi rdcwayx,

Thanks alot. What is the meaning of $NF FS=_ and OFS_ ?

I am new to shell.
# 5  
Old 11-15-2011
# 6  
Old 11-16-2011
Thanks..I found it already. Its awk program , where FS =field seperator and OFS is output field seperator. I learned something new today!! Thanks alot guys~~~
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

Replace a "space" for a string in while loop

I'm doing a curl sweep on a list of URLs in a .txt. Some of the URLs may have a "space" in them- which is OK for the browser (it throws in a %20), but not OK in bash. With my curl check, I need it to replace a "space" with a "%20" I was thinking about using the tr command but I don't know where... (7 Replies)
Discussion started by: sudo
7 Replies

5. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

6. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

7. UNIX for Dummies Questions & Answers

replace "," with "." only in specific columns of a file?

Hi all, I have this text file containing 9 columns separated by space. The 8th columns contains the numbers. C1 C2 C3 C4 C5 C6 C7 C8 C9 er rt yt gh iu nk il 0.07 xs yt lr ty bg iu zk nh 0,0005 lt ...etc. I want to replace the comma with full stop only in 8th coloumn. the output... (8 Replies)
Discussion started by: Unilearn
8 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. Shell Programming and Scripting

Script to search a string which is in between "" and replace it with another character

Hi, I am trying to search a string from a text file which is in between "" (Double Quotes) (Eg: "Unix"), and replace it with a | where ever it is appearing in the text file and save the file. Please help me. -kkmdv (6 Replies)
Discussion started by: kkmdv
6 Replies

10. Shell Programming and Scripting

Replace a blank space with string "\\ "

Hi, I have a requirement to replace a every blank space with char "\\ ". Like string "God Love" to "God\\ Love" and "God Love" as "God\\ \\ Love". and only in the sed. We have already a script but it is replaceing all continuous blank space with one "\\ ". which is as DIR=`sudo echo... (7 Replies)
Discussion started by: Akshay4u
7 Replies
Login or Register to Ask a Question