Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-01-2012
Registered User
 
Join Date: Jun 2012
Posts: 21
Thanks: 7
Thanked 0 Times in 0 Posts
look for specific values in a file and rename file with value found

Hi,
i have a file with some data ..look for some specific value in the file and if found that value rename the file with the value found in the file..
ex..
File.txt
1236 43715825601ANDERSSON,
1236 437158256031963040120060901200609010000000
1236 43715825604123 MCCL AVE UPPER
1236 43715825605AMSTERDAM NY 120100000
1236 43715825611A756
1236 43715825633100620120215I00000007819GOR
1236 43715825633100620120215F00000020850GOR
1237 11868184001BEAUREGARD, TAMMY L 0F
1237 11868184003196812272001113020011130000000000000000000
1237 118681840041566 SALT SPRINGVILLE
1237 11868184005FORT PLAIN NY 133390000
1237 11868184011A756
need to fine the value A756(in place of A756 we can have any of the value from these...A756 or B234 or C987 ) if we found any of the value with in the file the file name should be changed to File_ValueFound.txt
ex: File_A756.txt
the place of value is same always..

Thanks in advance..
Siva Santosh

Sponsored Links
    #2  
Old 07-02-2012
spacebar's Avatar
Registered User
 
Join Date: Oct 2009
Location: spaceBAR Central
Posts: 303
Thanks: 0
Thanked 59 Times in 59 Posts
Try this:

Code:
fv="A756"
v=`fgrep "$fv" test.txt`
if [[ -n $v ]]; then mv test.txt test_${fv}.txt; fi;

Sponsored Links
    #3  
Old 07-02-2012
amitranjansahu's Avatar
Registered User
 
Join Date: Jan 2009
Location: Bangalore,INDIA
Posts: 507
Thanks: 4
Thanked 27 Times in 27 Posts
The basic logic is bellow to find and rename files having a matching string. You can add multi sreach string as per your requirement.

PHP Code:
for filename in `grep -l AAA /root/ars/*`;
do
newname=$filename"AAA"
echo $newname
# move your file i=using mv command
done 

Last edited by amitranjansahu; 07-02-2012 at 01:15 AM.. Reason: code tag
    #4  
Old 07-02-2012
Registered User
 
Join Date: Jun 2012
Posts: 21
Thanks: 7
Thanked 0 Times in 0 Posts
@Spacebar..
we dont know which one will be in the file ..it is either A756 or B234 or C987
Sponsored Links
    #5  
Old 07-02-2012
Registered User
 
Join Date: Mar 2009
Location: Chennai, India
Posts: 12
Thanks: 0
Thanked 1 Time in 1 Post
Try this if the position of the records is always going to be in the 5th line and the string is 4 characters,


Code:
mv file.txt File_`sed -n '5p' file.txt | awk '{print substr($0,(length($0)-3))}'`.txt

Sponsored Links
    #6  
Old 07-02-2012
Registered User
 
Join Date: Jun 2012
Posts: 21
Thanks: 7
Thanked 0 Times in 0 Posts
Hi Athix,
the record position can be any where(not the 5th line each time) ...but the string length is always 4 characters..but if string exist it will be from 17th position from start of the line (any of the line)..
Sponsored Links
    #7  
Old 07-02-2012
jayan_jay's Avatar
Forum Advisor
 
Join Date: Jul 2008
Posts: 831
Thanks: 9
Thanked 185 Times in 176 Posts
Only these 3 strings A756 or B234 or C987 to check ??? If NO, then on what basis, we can get the values of string from the input file ..

Be clear while providing the input and dont let us assume. This would eaten up our time to provide answers ..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rename a file based on a specific separator sikilaklak Shell Programming and Scripting 3 01-20-2012 02:34 PM
[solved] how to get specific env variable values into a file crimso Shell Programming and Scripting 3 08-12-2011 09:48 AM
extract specific string and rename file mukeshguliao Shell Programming and Scripting 1 03-15-2011 08:03 AM
to extract specific values twice in a file techmoris Shell Programming and Scripting 7 08-17-2009 11:04 AM
finding specific values in a within a file Gerry405 UNIX for Dummies Questions & Answers 3 11-21-2005 10:37 AM



All times are GMT -4. The time now is 02:30 AM.