Convert case on specified position of flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert case on specified position of flat file
# 1  
Old 09-30-2008
Convert case on specified position of flat file

Please help

Need a script which will do the following :

Search on fixed width file , go to position (25,2) which means 25th and 26th position, Find if there are any char in lower case:

For example 25,2 can be (9T) or (9w) or (Ww) or (wW)....The two positions can be numeric or alpha...no special char,

Then should get converted to upper case, 9T, WW or IT.

So the output should be entire line where 25,26 positions are in upper case.
# 2  
Old 09-30-2008
_string="this is A Test scRipt fhefihgrewhio ewrfjoip";
change_start=25;
no=2;
change_end=$((change_start + no - 1))
str1=`echo ${_string} | cut -c1-$((change_start - 1))`
str2=`echo ${_string} | cut -c${change_start}-${change_end} | tr "[a-z]" "[A-Z]"`
str3=`echo ${_string} | cut -c$((change_end + 1))-`
echo "$str1$str2$str3"


try this...Im sure there might be simpler ways...
# 3  
Old 09-30-2008
this would work fine on single record...i have 400 million rows on which this should work
# 4  
Old 09-30-2008
Try it with awk:

Code:
awk '{print substr($0,1,24)toupper(substr($0,25,2))substr($0,27)}' file > newfile

Regards
# 5  
Old 09-30-2008
if you have PHP
Code:
<?php
$filename="file";
if ( $handle = fopen($filename,"r") ){
    while( ! feof($handle) ){
        $line = fgets($handle,4096);
        $toreplace = strtoupper(substr($line,24,2));        
        echo substr_replace($line,$toreplace,24,2);
    }   
    fclose($handle);
}
?>

# 6  
Old 09-30-2008
awk '{print substr($0,1,30)toupper(substr($0,31,2))substr($0,33)}' /tmp/test.dat > /tmp/abc.dat

tried on the below sting

10001da 20080925LHR BAH gf 0006V 20080818001SSA9122278L4NB 20250510 LHR

gave back the same....any mistake in the way i have changed code
# 7  
Old 09-30-2008
If the spaces in that example are correct, the substring at offset 31 is 6V which is already uppercase.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Convert flat file to csv

Hi I have a file like this: a=1 b=2 c=3 a=4 b=2 d=3 a=3 c=4 How can I change this to csv format a,b,c,d 1,2,3,, 4,2,,3 3,,4,, Please use code tags next time for your code and data. Thanks (10 Replies)
Discussion started by: sandip_2014
10 Replies

2. Shell Programming and Scripting

convert specific field to upper case in file

Hi Friends, I have a file1.txt as below I want to convert the date fields in to upper case field3 and field 6 output.txt Plz help. (2 Replies)
Discussion started by: i150371485
2 Replies

3. Shell Programming and Scripting

Find the position of a field/column in a flat file

Hi, Let say I have a file which has around 400 fields. SampleFile ========= PATIENTID|FACILITY|................|TIME_LAST_VISITED_BY_MD|.....|STATUS| How is it possible to find out which field is TIME_LAST_VISITED_BY_MD?fro example by seeing the above structure we can saw FACILITY... (5 Replies)
Discussion started by: machomaddy
5 Replies

4. Shell Programming and Scripting

Script to Convert Upper case to Lower case

Hi All I have a script which extracts values from a Database (A persons name) and puts it into a variable in my script IE: $NAME However the Value in the DB is all in uppercase and contains the users first name and last name EG: > echo $NAME GRAHAM BOYLE > What I need is only the... (7 Replies)
Discussion started by: grahambo2005
7 Replies

5. Shell Programming and Scripting

convert upper case to lower case in ascript

I have a package to install and the installation script which does it . The files/directories names in the script are all lower case but the actual package has everything in upper case - file names, directories . I don't want to rename directories and files in the package - it has a lot of them . ... (2 Replies)
Discussion started by: vz6zz8
2 Replies

6. Shell Programming and Scripting

how to convert value in a variable from upper case to lower case

Hi, I have a variable $Ctrcd which contains country names in upper case and i want to convert them into lower case. I have tried so many solutions from already existing threads but couldn't get the correct one. Can anybody help me with this..... Thanks a lot.. (2 Replies)
Discussion started by: manmeet
2 Replies

7. Shell Programming and Scripting

Awk to convert a flat file to CSV file

Hi , I have a file with contents as below: Contract Cancellation Report UARCNCL LOS CODE DATE REAS TYPE AMOUNT AMOUNT LETTER BY ========= ======= ==== ==== ==== ========= ==== ==== 8174739 7641509 1S NONE CRCD 30-JUN-2008 NPAR N .00 .00 CCAN 8678696 8091709 1S NONE DDEB 30-JUN-2008... (14 Replies)
Discussion started by: rkumudha
14 Replies

8. Shell Programming and Scripting

How to insert at a particular position in flat file

Hi All, I have a flat file with ~ as de-limiter (e.g: aaa~ba a~caa~0~d~e) What I want is check if the 4th character is 0 and replace it with say 4. So now it becomes : aaa~ba a~caa~4~d~e. I have to do this for the whole file, but the delimiter position remains the same, not the... (10 Replies)
Discussion started by: akdwivedi
10 Replies

9. Shell Programming and Scripting

Need help to convert Flat file to HTML

Hello I need help to convert flat file data to HTML Table format. I am generating everyday Flat file and want to convert into HTML Table format. The format of my file is: version host Total YRS NO APPS PSD 10 Sun 30 2 4 6 7 and flat... (11 Replies)
Discussion started by: getdpg
11 Replies

10. Shell Programming and Scripting

Sorting a flat file based on multiple colums(using character position)

Hi, I have an urgent task here. I am required to sort a flat file based on multiple columns which are based on the character position in that line. I am restricted to use the character position instead of the space and sort +1 +2 etc to do the sorting. I understand that there is a previous... (8 Replies)
Discussion started by: cucubird
8 Replies
Login or Register to Ask a Question