How to append a file extension to end of a file name??


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to append a file extension to end of a file name??
# 1  
Old 03-21-2007
How to append a file extension to end of a file name??

Hi,

I have a .txt file and it contains some file names.I want to append .gz extension to all the file names that are present within the .txt file.

Input.

aa.bb.Mar-20-2007
aa.cc.Mar-20-2007

Output

aa.bb.Mar-20-2007.gz
aa.cc.Mar-20-2007.gz

Please help me with this command.

Thanks,
Kumar
# 2  
Old 03-21-2007
Try this..
But not sure from your desc that the input file contains only filenames..

awk '{print $0".gz" }' original_file.txt > temp_file
mv temp_file original_file.txt
# 3  
Old 03-21-2007
Quote:
Originally Posted by jacoden
Try this..
But not sure from your desc that the input file contains only filenames..

awk '{print $0".gz" }' original_file.txt > temp_file
mv temp_file original_file.txt
If the input file contains other data besides the filenames, the below one will look for only those patterns which you mentioned above and rename it with .gz ext..

sed 's/\(.*\)\(-[0-3][0-9]-\)\([0-9]\{4\}\)/\1\2\3.gz/' original_file.txt > temp_file
mv temp_file original_file.txt
# 4  
Old 03-21-2007
Quote:
Originally Posted by jacoden
If the input file contains other data besides the filenames, the below one will look for only those patterns which you mentioned above and rename it with .gz ext..

sed 's/\(.*\)\(-[0-3][0-9]-\)\([0-9]\{4\}\)/\1\2\3.gz/' original_file.txt > temp_file
mv temp_file original_file.txt

Hi Jacoden,

Your first command with Awk is working perfectly fine.Can you please explain the awk and sed command with u had used.

Thanks a lot,
Kumar
# 5  
Old 03-21-2007
awk '{print $0".gz" }' original_file.txt > temp_file

$0 will be each line being processed by awk.So print $0" ".gz" will be a simple
concatanation of each line read from file with string ".gz" and printing it to stdout.


sed 's/\(.*\)\(-[0-3][0-9]-\)\([0-9]\{4\}\)/\1\2\3.gz/' original_file.txt > temp_file

Here, it looks for three pattern
\(.*\) - will look for any number of characters, followed by
(-[0-3][0-9]-\) - pattern like -20- (The date part), followed by
\([0-9]\{4\}\) - pattern like 2004 (or any year between 0000-9999)

And on successful match of the above reg exp,
replace the data with
\1 - will be the data which matched the first pattern.ie,\(.*\)
\2- as the second.ie,\(-[0-3][0-9]-\)
and \3 , the third one.ie,\([0-9]\{4\}\) and followed by string ".gz"

Hope this is clear.
# 6  
Old 03-21-2007
Quote:
Originally Posted by kumarsaravana_s
Hi,

I have a .txt file and it contains some file names.I want to append .gz extension to all the file names that are present within the .txt file.

Input.

aa.bb.Mar-20-2007
aa.cc.Mar-20-2007

Output

aa.bb.Mar-20-2007.gz
aa.cc.Mar-20-2007.gz

Please help me with this command.

Thanks,
Kumar
for this example :
Code:
# more file
aa.bb.Mar-20-2007
aa.cc.Mar-20-2007
# sed -i 's/$/\.gz/' file
# more file
aa.bb.Mar-20-2007.gz
aa.cc.Mar-20-2007.gz

# 7  
Old 03-21-2007
Quote:
Originally Posted by jacoden
awk '{print $0".gz" }' original_file.txt > temp_file

$0 will be each line being processed by awk.So print $0" ".gz" will be a simple
concatanation of each line read from file with string ".gz" and printing it to stdout.


sed 's/\(.*\)\(-[0-3][0-9]-\)\([0-9]\{4\}\)/\1\2\3.gz/' original_file.txt > temp_file

Here, it looks for three pattern
\(.*\) - will look for any number of characters, followed by
(-[0-3][0-9]-\) - pattern like -20- (The date part), followed by
\([0-9]\{4\}\) - pattern like 2004 (or any year between 0000-9999)

And on successful match of the above reg exp,
replace the data with
\1 - will be the data which matched the first pattern.ie,\(.*\)
\2- as the second.ie,\(-[0-3][0-9]-\)
and \3 , the third one.ie,\([0-9]\{4\}\) and followed by string ".gz"

Hope this is clear.

Thanks a lot Jacoden for your explanation.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append orginal file date to end of file

Trying to process 1000 or so files. Take original date and append to end of file. Like so: tstpls42.bas tstpls42.bas.Sep-11--2011 Been working along these lines: date=`ll tstpls42.bas |cut -c 46-57 |sed -e 's/]/\-/g' | grep -v '^$'` for i in *.bas ; do j=`ll $i /hpdump/b1 | awk... (1 Reply)
Discussion started by: joeadmin
1 Replies

2. Shell Programming and Scripting

Want it to read the file name and then append date stamp at the end of file?

I was thinking something like for i in `find . -name "*.log.Z"`; do mv $i name.log.Z or something like that? (3 Replies)
Discussion started by: xgringo
3 Replies

3. Shell Programming and Scripting

append | to the end of each data in a file

I have a file which has data in the below format: 7810902|6783014102| || |0| |0| |0| |0|||||T|04/13/2006||9423|7421||100|2006-04-13 16:50:28|||2006-04-13 16:50:28|n|51|-1||214 1089929|||||NewSpCreateAction request successful. Activity ID = <826528>||||100|n|2006-04-13 16:50:27|2006-04-13... (3 Replies)
Discussion started by: ankianand88
3 Replies

4. UNIX for Dummies Questions & Answers

How can I append a text at end of file after displaying the file

I have a file "sample.txt" with the content as below: Hi This is a Sample Text. I need a single command using cat which serve the following purpose. 1.display the contents of sample.txt 2.append some text to it 3. and then exit But, all should be served by a sinle command.:confused: (1 Reply)
Discussion started by: g.ashok
1 Replies

5. Shell Programming and Scripting

append a record at the end of a file

Hi all, i have to append a record at the end of the file(a file which is already with some records).how do i do?please help me? is there any way of doing this with "SED" command.i am not sure.plz help me on this. would appreciate your ideas!!!! bye rao. (3 Replies)
Discussion started by: raoscb
3 Replies

6. Shell Programming and Scripting

Append newline at the file end

Hi All, Is there any way to append a newline character at the end of a file(coma-separated file), through shell script? I need to check whether newline character exists at the end of a file, if it does not then append it. Regards, Krishna (1 Reply)
Discussion started by: KrishnaSaran
1 Replies

7. Shell Programming and Scripting

Append to end of each line of file without a temp file.

Hello I am trying to append an incrimenting number to the end of each line I have it working with a temp file. But I want to do this without a temp file. a=1 cat "file" | while read LINE do echo "$LINE, $a" >> filewithnumbers a=`expr $a + 1` ... (4 Replies)
Discussion started by: rorey_breaker
4 Replies

8. Shell Programming and Scripting

to append few chars at the end of a file

hi i want to open a file at runtime append few chars at the end of each line all these i want to have done automatically how to do it (2 Replies)
Discussion started by: trichyselva
2 Replies

9. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies

10. Shell Programming and Scripting

echo, append to end of file

I need the line printed with echo to append to eof of to exactly line, am i able to do that? i mean echo "sysctl -w lalala=1" > to end of file /etc/sysctl.conf or to the 21st line, if the line exist, open new line and insert text there. Thx.maybe i'm in wrong topic but anyway... (2 Replies)
Discussion started by: hachik
2 Replies
Login or Register to Ask a Question