![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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:
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. |
|
|||||
|
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. |
|
||||
|
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:
|
|
||||
|
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
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.) |
|
||||
|
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 Code:
input 23.4 123RT3kljfsd12.3saf 234sddksadfjLEOkjffs 234RT3fjsdka23.4fkdjs 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 | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|