Add text before extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add text before extension
# 1  
Old 05-04-2011
Add text before extension

Hi ,

I have a txt file , in which there are multiple lines, I want to add .1cdb before each .txt file.

Code:
Source data

DB_SOURCE=DWHCBU
#Generate_file=/root/cbu/src/ET_CDR_SCAN.txt(# is a part of the data)
TARGET=DWHEDW
#Generate_TG_FILE=/root/cbu/src/ET_CDR_SCAN.txt
DB_SOURCE=DWHCBU
#Generate_file=/root/cbu/src/ET_TEMP_CDR_SCAN.1cdb.txt(# is a part of the data)
TARGET=DWHEDW
#Generate_TG_FILE=/root/cbu/src/ET_TEMP_CDR_SCAN.txt
DB_SOURCE=DWHCBU
#Generate_file=/root/cbu/src/ET_CDR_TEMP_SCAN.txt(# is a part of the data)
TARGET=DWHBIW
#Generate_TG_FILE=/root/cbu/src/ET_CDR_TEMP_SCAN.txt


Code:
Target Data 


DB_SOURCE=DWHCBU
#Generate_file=/root/cbu/src/ET_CDR_SCAN.1cdb.txt(# is a part of the data)
TARGET=DWHEDW
#Generate_TG_FILE=/root/cbu/src/ET_CDR_SCAN.txt
DB_SOURCE=DWHCBU
#Generate_file=/root/cbu/src/ET_TEMP_CDR_SCAN.1cdb.txt(# is a part of the data)
TARGET=DWHEDW
#Generate_TG_FILE=/root/cbu/src/ET_TEMP_CDR_SCAN.txt
DB_SOURCE=DWHCBU
#Generate_file=/root/cbu/src/ET_CDR_TEMP_SCAN.1cdb.txt(# is a part of the data)
TARGET=DWHBIW
#Generate_TG_FILE=/root/cbu/src/ET_CDR_TEMP_SCAN.txt


if there is only one dot in the #Generate_file , then I need to insert the .1cdb before the .txt

--Mora
# 2  
Old 05-04-2011
Code:
sed '/^#Generate_file/ s/\(\.txt\)/.1cdb\1/' infile

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 05-04-2011
Code:
awk 'BEGIN{FS=OFS="\."}NF==2&&/^#Generate_file/{$NF="1cdb."$NF}1'

This User Gave Thanks to yinyuemi For This Post:
# 4  
Old 05-05-2011
Code:
sed 's/\.txt/\.1cdb\.txt/' infile > outfile

or if you prefer to remove the need for a temp file (outfile):

Code:
sed -i 's/\.txt/\.1cdb\.txt/' infile

You can even do a pretest and print to the screen first:

Code:
sed 's/\.txt/\.1cdb\.txt/' infile

This User Gave Thanks to ddreggors For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

3. Shell Programming and Scripting

How to add a text at the beginning of a text files in a folder?

how to add a text ( surya) at the beginning of a text files (so many) in folder text file: 111111 555555 666666 result: surya 111111 555555 666666 (3 Replies)
Discussion started by: suryanarayana
3 Replies

4. Shell Programming and Scripting

How to add the dat time stamp just before the last extension of a file?

hi, A file name is in the form inside a variable FILE_NAME="s1.txt.xls" i want to append date and time stamp inbetween s1.txt and .xls. so after appending date time stamp the file name would be MOD_FILE_NAME="s1.txt.201307251422.xls" currently i am using the below code... (4 Replies)
Discussion started by: Little
4 Replies

5. Shell Programming and Scripting

Use command to get a parameter and add extension

Hi, I'd like to get something like: Input: my_command FILEOutput to command line: my_command FILE.ext1 FILE.ext2explained: I want to provide a filename without extension and it should be given out like a command with the filename+extension1 and filename+extension2. I'd assume this... (3 Replies)
Discussion started by: unknown7
3 Replies

6. Shell Programming and Scripting

selecting files with text extension

I have a script that read some values from the properties file and move files from source folder to destination folder based upon the modification time of the files created (N configurable days), rite now it pick up all types of files from source folder and put it into destination folder the script... (2 Replies)
Discussion started by: nks342
2 Replies

7. Shell Programming and Scripting

Script to add extension to filename

Hi all, I have a folder with a bunch of files in them, and I would like to add an extension (.mp3)to all these filenames. The folder has only files that I'd like .mp3 added to. It looks something like this: Intput: File1 File2 File3Output: File1.mp3 File2.mp3 File3.mp3Thanks in... (2 Replies)
Discussion started by: repiv
2 Replies

8. Homework & Coursework Questions

how to change this looking for mimetype "text/plain" instead of extension *.txt?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Create a Shell script that looks for all text files in your home directory (including subdirectories). List... (3 Replies)
Discussion started by: rollinator
3 Replies

9. Solaris

removing particular lines ending with a .cnt extension in a text file

I have a text file with rows of information (it is basically a ls command information(o/p from ls command)) I need to remove the lines ending with a .cnt extension and keep the lines ending with .zip extension, how to accomplish this. I also only need the date,size and name of the file from every... (2 Replies)
Discussion started by: ramky79
2 Replies
Login or Register to Ask a Question