How to read contents in each file and rename the file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read contents in each file and rename the file?
# 1  
Old 12-03-2014
How to read contents in each file and rename the file?

Hello All,

Can you help me in writing a script for reading the specific position data in a file and if that data found in that file that particular file should be renamed.

Ex: Folder : C:\\test
and Filename : CLSACK_112214.txt,CLSACK_112314.txt,CLSACK_112414.txt
Contents in the file would be :
Code:
ISA* * * ~GS*CLS02*4010~ -> CLSACK_112214.txt
ISA* * * ~GS*CLS01*4010~ -> CLSACK_112314.txt
ISA* * * ~GS*CLS00*4010~ -> CLSACK_112414.txt

So first I need to read the first file CLSACK_112214.txt and in that I need to check conditions like

if the position from 15 to 21 == *CLS02*
then rename the current file CLSACK_112214.txt to UPDATEACK_112214.txt

Else if the position from 15 to 21 == *CLS01*
then rename the current file CLSACK_112314.txt to UPDATEACK_112314.txt

if the position from 15 to 21 == *CLS01*
then rename the current file CLSACK_112414.txt to UPDATEACK_112414.txt

So the script should read the files in the folder one by one and if any of the 3 conditions below gets satisfied then the current file should be rename from CLSACK_XXXXXX.txt to UPDATEACK_112414.txt and then it should read next file and another file until the files in the folders scanning gets completed.

So anyone can give a script for the above scenario , it should be really helpful.

Thanks
Nandu

Last edited by rbatte1; 12-03-2014 at 11:31 AM.. Reason: Spellings and adding formatting to clearly mark file names and strings to search for
# 2  
Old 12-03-2014
Few questions
  • Is the file containing only one line as you shown?, If not, explain the scenario
  • Date field should be taken from filename only right? but at the end, you said everything should be renamed as 112414!
  • Please re post the samples using code-tags as I couldnt find specific string at position 15-21

Last edited by rbatte1; 12-08-2014 at 09:12 AM.. Reason: Added LIST tags
# 3  
Old 12-05-2014
Code:
ISA*01*00        *01*00        *ZZ*DIEBOLDTEST    *ZZ*CELORGC03      *141203*1556*U*00401*000000306*1*T*^'GS*FA*DIEBOLDTEST*CLS01*20141203*1556*3*X*004010'

So the contents in the file would be in above manner and the file name would be in this manner CLSACK_XXXXXX.txt

Rename should be from CLSACK_XXXXXX.txt to UPDATEACK_XXXXXX.txt

Thanks

Last edited by Franklin52; 12-05-2014 at 08:56 AM.. Reason: Please use code tags
# 4  
Old 12-08-2014
Welcome nanduedi,

I don't see how your search will be effective on this most recent input, indeed in your first post you say that you are looking in columns 15-21 for one of three strings, but the input you have provided has that at column 13-19.

The actions are the same if you match any of the three conditions, so one statement should suffice if we can be sure of the criteria.

It would be useful if you can show what you have tried so far, tell us what tools you are most familiar with (so we write considering that you will have to look after this in future) and the version & level of OS you are running. From the directory name, I'm guessing that you are using an emulator under Windows, so that may pose other problems, but we'll address those if we hit them.



Regards,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename file in directory using contents within each file

In the below there are two generic .vcf files (genome.S1.vcf and genome.S2.vcf) in a directory. There wont always be two genaric files but I am trying to use bash to rename each of these generic files with specfic text (unique identifier) within in each .vcf. The text will always be different, but... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. UNIX for Beginners Questions & Answers

Shell Script to Read the given file contents into a merged one file

Like to have shell script to Read the given file contents into a merged one file with header of path+file name followed by file contents into a single output file. While reading and merging the file contents into a single file, Like to keep the format of the source file. ... (4 Replies)
Discussion started by: Siva SQL
4 Replies

3. UNIX for Beginners Questions & Answers

sed read contents of file and print value another file

Trying to use sed to insert the contents of a file into the end of each line in another file file1 This is a line Here is another line This is yet another line Here is a fourth line file2 TEXT desired output This is a line TEXT Here is another line TEXT This is yet another... (6 Replies)
Discussion started by: jimmyf
6 Replies

4. Shell Programming and Scripting

Remove or rename based on contents of file

I am trying to use the two files shown below to either remove or rename contents in one of those files. If in file1.txt $5 matches $5 of file2.txt and the value in $1 of file1.txt is not "No Match" then that value is substituted for all values in $5 and $1 of file2.txt. If however in $1 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

5. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

6. Programming

read() contents from a file

Hi, I'm trying to implement a C program on ubuntu which reads the contents of a file that is passed in as an argument and then displays it to the screen. So far I've cobbled together this from bits online but most of it is probably wrong as its all copied and pasted... #include <stdio.h>... (2 Replies)
Discussion started by: cylus99
2 Replies

7. Shell Programming and Scripting

how to read contents of file?

I have made a script something like this. I want it to read the contents of either file or directory but 'cat' and 'ls' is not working. Can anyone help me? I am a newbie in scripting so dont know much about it. I also dont know how can i put my code separatly on this forum #!/bin/bash echo... (9 Replies)
Discussion started by: nishrestha
9 Replies

8. Shell Programming and Scripting

How to read contents of a file into variable :(

My file is in this format : username : student information : default shell : student ID Eg : joeb:Joe Bennett:/bin/csh:1234 jerryd:Jerry Daniels:/bin/csh:2345 deaverm: Deaver Michelle:/bin/bash:4356 joseyg:Josey Guerra:/bin/bash:8767 michaelh:Michael Hall:/bin/ksh:1547 I have to... (1 Reply)
Discussion started by: dude_me5
1 Replies

9. Shell Programming and Scripting

Read contents from a file

Hi Friends, I am new to this forum. Just struck up with a logic. I have a csv file seperated by ":" (colons). This csv file contains hostname and groups as follows: HOSTNAME:VT Group SGSGCT2AVPX001:Team1 SGSGCT2AVPX003:Team2 SGSGCT2AVPX005:Team2 PHMNCTTAVPX001:Team3 I want to... (2 Replies)
Discussion started by: dbashyam
2 Replies

10. Shell Programming and Scripting

Read File and use contents to rename another

Hello guys, thank God that I found this forum. I hope that someone can help me because I don't have any idea on how to start it. I know that for some of you this is a very simple task but I'm not as advance on shell scripting like many people out there. I got this file with a permanent... (10 Replies)
Discussion started by: Shark Tek
10 Replies
Login or Register to Ask a Question