Parse and replace string in filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse and replace string in filename
# 1  
Old 05-27-2008
Parse and replace string in filename

Hi,

I've a filename of this format: "XXX_XXX_TXT.TAR.AS". Need to change the name into this format: "XXX_XXX.TAR.AS". This file resides in a directory. I'm ok with using the find command to search and display it.

Essentially I just need to replace the string "_TXT.TAR.AS" to ".TAR.AS". Is awk or sed the way to do it?

Thanks.
# 2  
Old 05-27-2008
[QUOTE=chengwei;302199456]Hi,

I've a filename of this format: "XXX_XXX_TXT.TAR.AS". Need to change the name into this format: "XXX_XXX.TAR.AS". This file resides in a directory. I'm ok with using the find command to search and display it.

Essentially I just need to replace the string "_TXT.TAR.AS" to ".TAR.AS". Is awk or sed the way to do it?

Thanks.[/QUOTE
first do a find and store the files in a new file. Loop it and inside loop do this.

Code:
sed 's/XXX_XXX_TXT.TAR.AS/XXX_XXX.TAR.AS/' filename

# 3  
Old 05-27-2008
With sed:

Code:
sed 's/\(.*\)_TXT\(.*\)/\1\2/'

With awk:

Code:
awk '{sub("_TXT","")}1'

Regards
# 4  
Old 05-27-2008
Quote:
Originally Posted by namishtiwari
Code:
sed 's/XXX_XXX_TXT.TAR.AS/XXX_XXX.TAR.AS/' filename

The values XXX_XXX are actually timestamp dynamically generated, hence I do not know them in advance. How would I be able to combine with the find command to achieve this?
# 5  
Old 05-27-2008
I did this:
Code:
find . -name "*_TXT.TAR.AS" -print -exec awk '{sub("_TXT.TAR.AS")}1' {} \;

and gotten the error messages:
Quote:
./XXX_XXX_TXT.TAR.AS
awk: syntax error near line 1
awk: bailing out near line 1
What am I missing here?

Many thanks for the help.
# 6  
Old 05-27-2008
Code:
find . -name "*_TXT.TAR.AS" -print|awk '{f=$0;sub("_TXT","");print "mv "f" "$0}'|sh

or:

Code:
find . -name "*_TXT.TAR.AS" -print |sed 's/\(.*\)_TXT\(.*\)/mv & \1\2/'| sh

Check the output before you pipe it to sh.

Regards
# 7  
Old 05-27-2008
Quote:
Originally Posted by Franklin52
With sed:

Code:
sed 's/\(.*\)_TXT\(.*\)/\1\2/'

With awk:

Code:
awk '{sub("_TXT","")}1'

Regards
What does the "1" in awk command meant? How does it change into the string that I want?

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To replace a filename

Hi i want to replace date stamp filename to original file name. i want to remove the datestamp after .txt For eg: file_name.txt_01-1002014 output: file_name.txt tried with below one but not able to achieve mv file_name.txt_01-1002014`basename "file_name.txt_01-1002014 "`.txt (4 Replies)
Discussion started by: rohit_shinez
4 Replies

2. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

4. Shell Programming and Scripting

parse a mixed alphanumeric string from within a string

Hi, I would like to be able to parse out a substring matching a basic pattern, which is a character followed by 3 or 4 digits (for example S1234 out of a larger string). The main string would just be a filename, like Thisis__the FileName_S1234_ToParse.txt. The filename isn't fixed, but the... (2 Replies)
Discussion started by: keaneMB
2 Replies

5. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

6. Shell Programming and Scripting

How to parse filename and one level up directory name?

Hello Experts, I need little help with parsing. I want to parse filename and one level up directory name. sample $1 will consists of /home/username/ABC1/rstfiles4.log /home/username/ABC4/rstfiles2.log /home/username/EDC7/rstfiles23.log /home/username/EDC6/rstfiles55.log... (8 Replies)
Discussion started by: Shirisha
8 Replies

7. UNIX for Dummies Questions & Answers

replace multiple patterns in a string/filename

This should be somewhat simple, but I need some help with this one. I have a bunch of files with tags on the end like so... Filename {tag1}.ext Filename2 {tag1} {tag2}.ext I want to hold in a variable just the filename with all the " {tag}" removed. The tag can be anything so I'm looking... (4 Replies)
Discussion started by: kerppz
4 Replies

8. Shell Programming and Scripting

Parse string

Hi, I need to parse a string, check if there are periods and strip the string. For example i have the following domains and subdomains: mydomain.com, dev.mydomain.com I need to strip all periods so i have a string without periods or domain extensions: mydomain, devmydomain. I use this for... (12 Replies)
Discussion started by: ktm
12 Replies

9. Shell Programming and Scripting

how to parse this string

I want to get filenames from the following input. How can I parse this in bash. input data ------------------------------------------------------------------- path=/aaa/bbb/filename1;/aaa/filename2;/aaa/bbb/ccc/ddd/filename3 -------------------------------------------------------------------... (13 Replies)
Discussion started by: hcliff
13 Replies

10. Shell Programming and Scripting

need help on sed (replace string without changing filename)

I have awhole bunch of files and I want to edit stringA with stringB without changing the filename. I have tried the following sed commands: sed "s/stringA/stringB/g" * This will print the correct results but does not actually save it with the new content of the file. when I do a cat on... (5 Replies)
Discussion started by: jjoves
5 Replies
Login or Register to Ask a Question