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
# 8  
Old 09-30-2008
cat file | while read line
do
_string="$line"
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"
done

I know this is not the way....if u try this logic with 400 million rows then it'd take years to complete...but anyway I felt like postin it
# 9  
Old 09-30-2008
Code:
perl -pe 'substr($_,30,2) = uc(substr($_,30,2))' file

or

Code:
perl -pe 's/^(.{30})(.{2})(.*)/\1\U\2\E\3/' file

Some new sed variants have \u as well, though I don't know if they have \e too.

Last edited by era; 09-30-2008 at 06:25 AM.. Reason: s/// with \U and \E too
# 10  
Old 09-30-2008
I am looking @ gf which has to get converted to uppercase
there are 4 spaces after 10001da, 2 spaces after LHR and BAH.
then gf would come to 30th position

"10001da 20080925LHR BAH gf 0006V 20080818001SSA9122278L4NB 20250510 LHR"
# 11  
Old 09-30-2008
Quote:
Originally Posted by ssantoshss
I am looking @ gf which has to get converted to uppercase
there are 4 spaces after 10001da, 2 spaces after LHR and BAH.
then gf would come to 30th position

"10001da 20080925LHR BAH gf 0006V 20080818001SSA9122278L4NB 20250510 LHR"
Then your command should looks like:

Code:
awk '{print substr($0,1,29)toupper(substr($0,30,2))substr($0,32)}' /tmp/test.dat > /tmp/abc.dat

Regards
# 12  
Old 09-30-2008
Use code tags to preserve spaces in samples. If you know the correct offsets, just change the scripts. If you don't know the exact offset, maybe you can change the script to only print the part you think is the correct offset, and see if it's right, or off by a few characters.
# 13  
Old 09-30-2008
just did this

awk '{print toupper(substr($0,30,2))}' /tmp/test.dat

o/p was just

gf

toupper doesnt seem to work. I am on Solaris 10 does this have any impact ?
# 14  
Old 09-30-2008
Hi All

I think i found the problem, the other varient of awk worked fine for me.

nawk '{print toupper(substr($0,30,2))}' /tmp/test.dat

o/p == GF

Thanks a lot for all the help.
Regards
Santosh S
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