|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Try this: Code:
fv="A756"
v=`fgrep "$fv" test.txt`
if [[ -n $v ]]; then mv test.txt test_${fv}.txt; fi; |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
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:
Last edited by amitranjansahu; 07-02-2012 at 01:15 AM.. Reason: code tag |
|
#4
|
|||
|
|||
|
@Spacebar..
we dont know which one will be in the file ..it is either A756 or B234 or C987 |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|