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
read file until certain line position finalight Shell Programming and Scripting 5 05-21-2008 03:16 AM
Read value from particular position in file. krishnarao Shell Programming and Scripting 2 05-15-2008 06:49 AM
read specific text from a log file ragha81 Shell Programming and Scripting 4 10-17-2006 01:17 PM
read or search the item in a file sequentially by position using unix shell script? lok UNIX for Dummies Questions & Answers 6 07-12-2006 06:53 AM
Text replace by position instead of reg expr. videsh77 Shell Programming and Scripting 4 04-11-2005 09:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-17-2008
COD COD is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 13
read space filled file and replace text at specific position

Hi I have a spaced filled file having records like below:

What I want is to read line having RT3 at position 17-19 then go to position 2651 check the 18 characters (might be space filled till 18 characters). This position should have a decimal number, if it has whole number then replace this number with number in a variable say "chrg" and write to another file.

When we read this space filled file the read command truncates the spaces, so we cannot find the contents in specific position. As we want to write back after replacing the number, the new file should be same as pervious file (space filled) so that it can be used by another program.

Quote:
2791U00218110408RT31007801870070102120320081203200800000000|||||||||||1603661||||||||||||||||||||||| |||||||516636|||||||||||||39707||||||||||||476929||||||||||||||||||||||||||||||||2022||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||0||||||||||||||||| |||||||||||||||||||||||||||||||478951||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||0||||||||||||478951||||||||||||||||||||||||||||||||||||||||||||||| |478951||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||2006||||||||||||398907|||||||||||||30.00|||||||||119672.10||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||2007||||| ||||||||80044|||||||||||||30.00||||||||||24013.20||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||143685.30||||||||||27652.25||||||| |||||||||||||||||||||||||0.00|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||116033.0 5|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||116033.05||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||116033.05||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||11 6033.05||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||10048||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||||||41812||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||0|||||||||||||||||X|||||||||||||||||||||||||||116033.05||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||478951|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||
where | here has repalced spaces for more clarification.

The position 2651 here is shown in bold as we want to replace this string with some other variable but only for RT3 records. There are other records also but they dont have RT3 string in them. The file content shown above is one big whole string.

Please advise.
  #2 (permalink)  
Old 04-17-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
You can try something like that (not tested) :
Code:
awk -v new="$chrg" '
substr($0, 17, 3) == "RT3" {
   if (substr($0, 2651, 18) ~ /^[0-9]* *$/) 
      printf("%s%-18.18s%s\n", substr($0, 1, 2650), new, substr($0, 2669));
   next;
}
1
' inputfile
Jean-Pierre.
  #3 (permalink)  
Old 04-17-2008
COD COD is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 13
Can you explain the logic as I am new to AWK
  #4 (permalink)  
Old 04-17-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
awk -v new="$chrg" '
. . .
' input file


Run AWK, the executed program in ' . . .', the input file is inputfile and the variable new is defined.

substr($0, 17, 3) == "RT3" {
. . . .
}


Select lines with RT3 in pos 17 (length 3), and execute { . . . . }

if (substr($0, 2651, 18) ~ /^[0-9]* *$/)
printf("%s%-18.18s%s\n", substr($0, 1, 2650), new, substr($0, 2669));


If the record contains digits and spaces at position 2651 (length 18), print
the record with value of variable new in position 2651 (length 18).

next

Proceed next record

1

Print record (records without RT3)


jean-Pierre.
  #5 (permalink)  
Old 04-17-2008
COD COD is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 13
Thanks for the explanation, I have tested the script and there is problem that it replaces the previous string values with spaces and the position is written by the new variable.

This is the output
Quote:
2791U00218110408RT31010105114190102151120071911200709042008||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||||||||||||||||||||||||||||||||||||||||||||||||||166031.25||||||||||||||||||||||||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  #6 (permalink)  
Old 04-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Can you try with another version of awk? Do you have nawk, or mawk, or gawk?

Or try Perl:

Code:
perl -pe 'if (substr($_,16,3) eq "RT3" && substr($_,2560,18) =~ /^\d+ *$/) {
  substr($_, 2560, 18) = sprintf("%-18i", $ENV{"chrg"});
}' file
... assuming chrg is an integer. Or simply use the %-18.18s format suggested by aigles.

If this doesn't work for you, either, maybe you could attach a small sample file to test with. The sample you posted only has spaces at position 2651 in the version I copy+pasted from here. The number 478951 is at position 2690 in my copy. (In Perl, these numbers are one less, because substr counts offsets from zero, not positions.)
  #7 (permalink)  
Old 04-21-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
hi,
I am not sure whether understood your requirement totally and exactlly.

Anyway, hope below one can help you a little.

file:
Code:
123RT3kljfsd12.3saf
234sddksadfjLEOkjffs
234RT3fjsdka1234fkdjs
output:
Code:
input 23.4
123RT3kljfsd12.3saf
234sddksadfjLEOkjffs
234RT3fjsdka23.4fkdjs
code:
Code:
read t
cat file | nawk -v tt="$t" '{
if(substr($0,4,3)=="RT3")
{
	temp=substr($0,13,4)
	if (temp+0==temp && index(temp,".")==0)
		printf("%s%s%s\n",substr($0,1,12),tt,substr($0,17))
	else
		print
}
else
	print
}'
Sponsored Links
Closed Thread

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 11:24 AM.


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