The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to write the file by position ford2020 Shell Programming and Scripting 1 05-03-2009 12:42 AM
find if a position is between a given start and end position fadista UNIX for Dummies Questions & Answers 9 12-04-2008 12:17 PM
how to find a position and print some string in the next and same position naveenkcl Shell Programming and Scripting 1 08-21-2008 01:18 PM
How to get the position of a file ravi214u Shell Programming and Scripting 3 07-07-2008 11:10 AM
Read value from particular position in file. krishnarao Shell Programming and Scripting 2 05-15-2008 06:49 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-12-2009
angelarosh angelarosh is offline
Registered User
  
 

Join Date: May 2009
Posts: 13
Replacing a position in a file

Hi
I'm trying to use awk in a file(test123.dat).
My requirement is to to check for the 65th position, if the 65th position is a space then replace the 65th position by the number 9.

This is the code that i used:
awk '{substr($0,65,1) ~ / / }{sub(substr($0,65,1),"9")}{print}' test123.dat

But this doesnt work
Plz Help...
  #2 (permalink)  
Old 06-12-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
A little change...
Code:
awk '{if(substr($0,65,1)==" ")sub(substr($0,65,1),"9");print}'
-----Post Update-----

by the way
this was wrong
Code:
 (substr($0,5,1) ~ / / )
-----Post Update-----

by the way
this was wrong
Code:
 (substr($0,5,1) ~ / / )
  #3 (permalink)  
Old 06-12-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Code:
 
 
echo "0049DA06152009FPA06USD 000000000000D000000000009.9" | awk '{ print length($0) }'
50
r you trying to modifi 65th character ?..
  #4 (permalink)  
Old 06-12-2009
angelarosh angelarosh is offline
Registered User
  
 

Join Date: May 2009
Posts: 13
This is a sample data from the file test123.dat

0049DA06152009FPA06USD 000000000000D000000000009.9

If i use awk '{if(substr($0,65,1)==" ")sub(substr($0,65,1),"9");print}' test123.dat

this is the result that i get, the "9" gets appended after USD

0049DA06152009FPA06USD9 000000000000D000000000009.9

but i should get the result

0049DA06152009FPA06USD 000000000000D000000000009.99
  #5 (permalink)  
Old 06-12-2009
angelarosh angelarosh is offline
Registered User
  
 

Join Date: May 2009
Posts: 13
I had pasted only a part of the data in my previous post, sorry abt it

This is the actual data in the file

0049DA06152009FPA06USD 000000000000D000000000009.9 ORA100144

The total length is 149 and im trying to do the changes in the 65th position

When i post the actual data the spaces/tabs in the actual data does not get posted.
  #6 (permalink)  
Old 06-12-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
try something like this
Code:
 
 echo "0049DA06152009FPA06USD 000000000000D000000000009.9 ORA100144" |
awk 'substr($0,51,1) ~/[^0-9A-Z]/ {print substr($0,1,50)"9"substr($0,52)}'
  #7 (permalink)  
Old 06-12-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by angelarosh View Post
I had pasted only a part of the data in my previous post, sorry abt it

This is the actual data in the file

0049DA06152009FPA06USD 000000000000D000000000009.9 ORA100144

The total length is 149 and im trying to do the changes in the 65th position

When i post the actual data the spaces/tabs in the actual data does not get posted.
First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:23 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0