Awk command to replace specific position characters.
Hi,
I have a fixed width file.
The way this file works is say for example there are 30 columns in it each with different sizes say 10,5,2, etc...
If data in a field is less than the field size the rest of it is loaded with spaces.
I would like an awk command to that would replace
Quote:
position 96-98 with "ABC"
position 99-113 with "DEF" rest of it with spaces
position 190-198 with "XYZ" rest of it with spaces
I have tried following but doesnt work
Please let me know with the correct command.
---------- Post updated at 07:34 PM ---------- Previous update was at 06:35 PM ----------
Thanks Chubler_XL. This works.
Sir could you please explain what we are doing in this.
I understand that we defined a function called "repl" which takes four parameters.
And then calling it 3 times.
I dont understand the following:
Quote:
sprintf("%-*s", t-f+1, v) substr(s,t+1) ---> What does this part does
we are storing all the return values in "a" and printing it. So will the values not get overwritten.
a=repl($0,96,98,"ABC")
a=repl(a,99,113,"DEF")
a=repl(a,190,198,"XYZ")
print a
And also when we are replacing characters from 99 to 113 with "DEF" without trailing spaces. How does it not mess the format.
Would really appreciate if you could explain this to me.
That's an awesome solution. I will try to analyze it, Chubler please correct if I mess up along the way
Let's look at the sprintf call:
The format flags of sprintf mean the following:
Quote:
- Left-justify within the given field width
* The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
So (t-f+1) is the width of the string v to be printed. Now the repl() calls:
takes a string s, position "from" f, position "to" t, and what to insert "v". It will only modify the string from f to t, inserting v properly padded.
will take the whole line ($0) and replace the characters 96-98 with "ABC".
will take a and replace chars 99-113 with "DEF" left aligned and padded.
will replace chars 190-198 of a with "XYZ" left-aligned and padded as needed.
So it is modifying the variable a, one step at a time, and taking the output of previous call as input for next one. The same 3 calls could be written nested as:
But we can all agree, that the former is much more readable, debuggable and commentable.
substr(s,1,f-1) --> line before the string that needs to be replaced
sprintf("%-*s", t-f+1, v) --> here "s" represents the entire line. t-f+1 lenght of string being replaced, v is the new string to be inserted. But where are we saying the start and end position to the sprintf function for the new string to be inserted. We are just passing the length of the new string wanted.
Here is where i need little more help. I looked at the sprint function man page but is not of much help.
substr(s,t+1) --> line after substring to be replaced.
And also we are not using any concatenate function to join various substrings. But in sql and other Datawarehouse tools we use concatenate function.
sprintf("%-*s", t-f+1, v) --> here "s" represents the entire line
No, 's' in the format stands for "string". It tells sprintf that the argument to be printed is a string (as opposed to %d which would expect a decimal number or %f which would expect a float).
So
is printing the string stored in variable v, with the format "%-*s", t-f+1 telling sprintf to justify left (-) and pad with (t-f+1) spaces.
Space concatenates strings in awk. So
will produce "onetwo"
Hi,
Im using sed on an AIX machine. I am trying to change the 137-139 characters if they are a ' 36'/'000' to a '036'. The positions that need to be changed are fixed.
the source data that I have is$cat v.txt
4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY... (9 Replies)
Discussion started by: dsid
9 Replies
2. Post Here to Contact Site Administrators and Moderators
In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123.
So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Need command for position based replace:
I need a command to replace with 0 for characters in the positions 11 to 20 to all the lines starts with 6 in a file.
For example the file ABC.txt has:
abcdefghijklmnopqrstuvwxyz
6abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz... (4 Replies)
Hi,
I am trying to get an output like :
+----------------------------------+ ----------- +
+ some variable substitution + some text +
Is there a way I can specify in printf (in ksh) the particular position I want to print a character, and also repeat a character from... (1 Reply)
How to replace the position specific values in the file..
i searched a lot the forums but i couldn't able to do...
i have file like below
576666666666666666666666666 7878 897987 121 0asdas Y12
5900fbb 777 09JJJ 78798347892374 234234234364 234232898
89HJHIHIGIUG989902743748327khjkhkjlh... (6 Replies)
I asked this before, but my problem got more complicated. Heres what I am trying to do:
I'm trying to replace a string at a certain location with another string.
Heres the file I'm trying to change:
\E
I want to replace the escape code at the 3rd line, 2nd column with this escape code... (3 Replies)
I'm drawing a blank on how to use sed to replace selectively based on position in the string (vs nth occurence):
hello.|there.|how.|are.|you.|
I want the period removed in the 3rd item (as defined by the pipe delimiter) if a period is present. So the result in this case would be:
... (2 Replies)
Hello
I want to add some value at the specific position.
My file has data like
Hello
Welcome to UNIX Forums
Need Assistance
I want to add some value at the end but at same character position for all lines.
I want my output file to have data like :
Here '_' represents blanks.... (3 Replies)
I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done?
Example:
File:
A0199999123
A0199999124
A0199999125
Need to replace 99999 in positions 3-7 with 88888.
Any help is appreciated. (5 Replies)
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... (6 Replies)