remove the number from the String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove the number from the String
# 1  
Old 02-22-2011
remove the number from the String

I have string like 20091112_File_Name_40301.txt
and i have a set of files in the directory with the same format . i want to write the ksh to rename the file ..... like

eg
20091112_File_Name_40301.txt to File_Name.txt
20091112_abc_2343.txt to abc.txt
20091112_xyz_2343.txt to xyz.txt

Thanks
Mani
# 2  
Old 02-22-2011
Code:
echo "20091112_File_Name_40301" |sed -r 's/(.*[0-9])_(.*)_([0-9]*)/\2/'
File_Name

# 3  
Old 02-22-2011
Code:
# a=20091112_abc_2343.txt
# b=`echo $a | tr -d '[0-9]_'`
# echo $b
abc.txt

---------- Post updated at 08:18 PM ---------- Previous update was at 08:16 PM ----------

Code:
for i in *.txt
do
     b=`echo "$i" | tr -d '[0-9]_'`
     mv "$i" "$b"
done

or better (see alister explanation)

Code:
for i in *.txt
do
     b=${i#*_}
     mv "$i" "${b%_*}.txt"
done


Last edited by ctsgnb; 02-22-2011 at 04:03 PM..
This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 02-22-2011
Code:
for fsrc in *.txt; do
    fdst="${fsrc#*_}"
    fdst="${fdst%_*}.txt"
    mv "$fsrc" "$fdst"
done

Regards,
Alister

---------- Post updated at 02:28 PM ---------- Previous update was at 02:25 PM ----------

Quote:
Originally Posted by ctsgnb
Code:
tr -d '[0-9]_'

The filename examples in the original post contain underscores. Your approach will mangle them. That said, it could be that those examples are incongruent with the actual data. Just a heads up in case it's important.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 02-22-2011
@ Alister

Yes of course ... look below Smilie

@gavemani

Please not that if you have a filename like
Code:
8593425832_anything_blablabla_whateverstring_90543902.txt

All the underscore will be deleted with the solution i posted, the target name will be :
Code:
anythingblablablawhateverstring.txt

instead of :
Code:
anything_blablabla_whateverstring.txt

This is what Alister wanted me to point out ... now it is done Smilie
# 6  
Old 02-22-2011
Code:
$ echo 20091112_File_Name_40301.txt |sed 's/[0-9]//g; s/^_*//;s/_*\./\./'
File_Name.txt

$ echo 20091112_abc_2343.txt |sed 's/[0-9]//g; s/^_*//;s/_*\./\./'
abc.txt

$ echo 20091112_xyz_2343.txt  |sed 's/[0-9]//g; s/^_*//;s/_*\./\./'
xyz.txt

# 7  
Old 02-22-2011
Things to watch out for:
  • filenames with _
  • filenames with spaces
  • filenames with numbers
  • filenames with extention other than .txt
  • filenames already stripped
First number looks like an 8 digit date so I tried to match it exactly:
Code:
$ ls -1
File_Name.txt
20100212_file_12_494849.txt
file_10.txt
19930101_xyz_292829.prn
 
$ ls | sed -E 's/^[1-9][0-9]{7}_(.*)_[0-9][0-9]*\.([^\.]*)/\1.\2/'
File_Name.txt
file_12.txt
file_10.txt
xyz.prn

So rename code would be:
Code:
for file in *
do
    new=$(echo "$file" | sed -E 's/^[1-9][0-9]{7}_(.*)_[0-9][0-9]*\.([^\.]*)/\1.\2/')
    [ "$file" = "$new" ] || mv "$file" "$new"
done


Last edited by Chubler_XL; 02-22-2011 at 08:50 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. UNIX for Beginners Questions & Answers

Remove string between number and character

hello ! I have to remove string between a number and set of characters. For example, 35818 -stress - - -stress - - - - - - DB-3754 44412 caul kid notify DB-3747 54432 roberto -, notify DB-3725 55522 aws _ _int _ _classified 2_a _a 2_m _m 2_classified 2_search... (7 Replies)
Discussion started by: ManoharMa
7 Replies

3. Solaris

Remove number in file name

Hi , i have a file name with date and time append like test_SEC_AES_V1_T03_2016031404306 i want to remove the date and time after _ and rename to current date and time,can any one please let me know as i tried some options but din't help:( Thanks, Please use code tags as required... (10 Replies)
Discussion started by: caba_jones
10 Replies

4. Shell Programming and Scripting

Remove trailing number

I have some strings such as ABC1 ABC2 TYFASDD12 They will only have letters and numbers. In each case I want to remove the last digit? The lengths will vary. So a hard coded substr won't work. What do I do? if it doesn't end in a number, I don't want to remove any characters. (6 Replies)
Discussion started by: guessingo
6 Replies

5. Shell Programming and Scripting

Remove not only the duplicate string but also the keyword of the string in Perl

Hi Perl users, I have another problem with text processing in Perl. I have a file below: Linux Unix Linux Windows SUN MACOS SUN SUN HP-AUX I want the result below: Unix Windows SUN MACOS HP-AUX so the duplicate string will be removed and also the keyword of the string on... (2 Replies)
Discussion started by: askari
2 Replies

6. Shell Programming and Scripting

Remove last number from a file

Hi, I need to delete last number ( i.e 126) from the file which has below contents. ABC DEF "XYZ" 2837.5 3852.5 126 ABC DEF "XYZ" 2897.5 3852.5 126 ABC DEF "XYZ" 2957.5 3852.5 126 Please help. Thanks in advance. (17 Replies)
Discussion started by: gujrathinr
17 Replies

7. Shell Programming and Scripting

Remove multiple lines from a particular string to particular string

Hi, I have a file containing the DDLs of tables in a schema. From that I need to remove all the lines from a starting string till a specific string. Here is an example. File1.txt ------------- CREATE TABLE "SCHEMA1"."LKP11_TBL_USERS" ( "ID" NUMBER(8,0) NOT NULL ENABLE, "USER_ID"... (3 Replies)
Discussion started by: satyaatcgi
3 Replies

8. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

9. Shell Programming and Scripting

remove characters from string based on occurrence of a string

Hello Folks.. I need your help .. here the example of my problem..i know its easy..i don't all the commands in unix to do this especiallly sed...here my string.. dwc2_dfg_ajja_dfhhj_vw_dec2_dfgh_dwq desired output is.. dwc2_dfg_ajja_dfhhj it's a simple task with tail... (5 Replies)
Discussion started by: victor369
5 Replies

10. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies
Login or Register to Ask a Question