File and file content string rename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File and file content string rename
# 1  
Old 12-08-2017
File and file content string rename

We have around 1000 scripts in a specific folder like mcx001.txt, mcx002.txt, mcx999.txt, mce001.txt etc
We need to rename those files where 3rd character is "x" and should be replaced by "y".
So files should be renamed to mcy001.txt, mcy002.txt, mcy999.txt

Also, mcx001.txt have content "filename=mcx001" which also needs to be renamed to "filename=mcy001".
This applies to all the files which have 3rd character as "x".
# 2  
Old 12-08-2017
Any attempts / ideas / thoughts from your side?
# 3  
Old 12-08-2017
Code:
for i in *w*.txt; do mv -v "$i" $(echo "$i" | tr 'w' 'd'); done

something like this?
and using sed to replace the content?

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-08-2017 at 05:01 AM.. Reason: Added CODE tags.
# 4  
Old 12-08-2017
There's no w nor d in your first specification - I reckon you mean x and y ? And, you don't seem to care for the character position, a w in any position will make that respective file a candidate for the modifications.

How about
Code:
awk '
FNR == 1        {print "rm " FILENAME > "remove_old_files.sh"
                 sub ("x", "y", FILENAME)
                }
/filename=/     {sub (/=.*/, "=" FILENAME)
                 sub (".txt", _)
                }
                {print > FILENAME
                }
' ??x*.txt

If happy with the results, run / source the remove_old_files.sh script (after having set the correct permissions, of course)
# 5  
Old 12-08-2017
Thanks!
I did not understand below
print "rm " FILENAME > "remove_old_files.sh"
# 6  
Old 12-08-2017
Did you look into the resulting file?
You want to remove the respective files after they have been modified and written to the new file names.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed, awk or another bash command to modify string with the content of another file

Hello everybody, I would like modify some strings using sed or another command line with the content file. For example: - {fqdn: "server-01" , ip: "server-01"} - {fqdn: "server-02" , ip: "server-02"} - {fqdn: "server-03" , ip: "server-03"} - {fqdn: "server-04" , ip: "server-04"} My... (4 Replies)
Discussion started by: dco
4 Replies

2. Shell Programming and Scripting

awk : split file and rename and save in path according to content

Hello, I'm using Windows 7 ; sed, awk and gnuwin32 are installed. I have a big text file I need to manipulate. In short, I will have to split it in thousands of short files, then rename and save in a folder which name is based upon filename. Here is a snippet of my big input.txt file (this... (4 Replies)
Discussion started by: sellig
4 Replies

3. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

4. Shell Programming and Scripting

Replace string in a file with some content indexed from another file.

We have two files file 1: (usually small, ~100 lines), each line contains a : separated index, value e.g 2: Apple 1: Banana 5: Pear 7: Orange File 2: (usually large, 10 million lines or more), each line contains a single string value. e.g xyz1 xyz2 xyz3 xyz4 xyz5 xyz6 xyz7 Now... (2 Replies)
Discussion started by: AlokKumbhare
2 Replies

5. Shell Programming and Scripting

Replace string by file content

hi I have template file my.tpl: bla-bla-bla <link href="style.css" type="text/css"> bla-bla-bla and style.css : body{margin: 0px;} I want to get in result one file: bla-bla-bla <script>body{margin: 0px;}</script> bla-bla-bla I tryed to used SED: sed '/<link .*href=\"(*)*\"... (6 Replies)
Discussion started by: dim_nsk
6 Replies

6. Shell Programming and Scripting

extract specific string and rename file

Hi all, I am working on a small prog.. i have a file.txt which contains random data... K LINES V4 ADD CODE `COMPANY` ADD CODE `DISTRIBUTOR` SEQ NAME^K LINES V5 SEQ NAME^K LINES V6 ADD `PACK-LDATE` SEQ NAME^K^KCOMMAND END^KHEADINFO... (1 Reply)
Discussion started by: mukeshguliao
1 Replies

7. Shell Programming and Scripting

Help with rename header content based on reference file problem

I got long list of reference file >data_tmp_number_22 >data_tmp_number_12 >data_tmp_number_20 . . Input file: >sample_data_1 Math, 5, USA, tmp SDFEWRWERWERWRWER FSFDSFSDFSDGSDGSD >sample_data_2 Math, 15, UK, tmp FDSFSDFF >sample_data_3 Math, 50, USA, tmp ARQERREQR . . Desired... (7 Replies)
Discussion started by: perl_beginner
7 Replies

8. Shell Programming and Scripting

how to add string inside the file's content

Hi, How to add string inside the file. In this case adding 63 in the beginning of each line. Thnks. e.g. what is inside the file Mobile Number Portability 9051151234 9051261345 9051393245 9051412345 9051507654 should like this as output: Mobile Number Portability (7 Replies)
Discussion started by: shtobias
7 Replies

9. Shell Programming and Scripting

Search for string in filename, not file content

How can I search for a string in a filename? For example, I want to know if a filename (not the file contents) contains the string "test". (3 Replies)
Discussion started by: daflore
3 Replies

10. Shell Programming and Scripting

replace a string with content from another file

Hi, I'm a newbi in shell script. Here what I want to do: FileA: bor bor bor xxxx bib bib bi FileB: something something something I want to replace string "xxxx" in FileA with contents of FileB. i tried with sed: fileb=`cat FileB` reg=xxxx file=FileA (4 Replies)
Discussion started by: afatguy
4 Replies
Login or Register to Ask a Question