I have a fixed with file with header & trailer length having the same length of the detail record file.
The details record length of this file is 24, for Header and Trailer the records will be padded with spaces to match the record length of the file
Currently I am adding 3 spaces in header and 6 spaces in trailer to match with detail record length
current value of my header is
Column Name Data length
---------- ----------
A 1
B 2
C 2
D 8
E 8
current value of my trailer is
Column Name Data length
---------- ----------
A 1
B 2
C 15
My requirement is to change the value of the second column(Column B) from xx to yyy both in header and trailer.
After this change I should have only two spaces in header and 5 spaces in trailer
Can some one help here please
---------- Post updated at 05:03 AM ---------- Previous update was at 02:43 AM ----------
I have a fixed with file with header & trailer length having the same length of the detail record file.
The details record length of this file is 24, for Header and Trailer the records will be padded with spaces to match the record length of the file
Currently I am adding 3 spaces in header and 6 spaces in trailer to match with detail record length
current value of my header is
Column Name Data length
---------- ----------
A 1
B 2
C 2
D 8
E 8
current value of my trailer is
Column Name Data length
---------- ----------
A 1
B 2
C 15
My requirement is to change the value of the second column(Column B) from xx to yyy both in header and trailer.
After this change I should have only two spaces in header and 5 spaces in trailer
Thanks for the reply.
Let me explain the requirement again
I have a file with header and trailer which is fixed width
Header is having five columns & trailer is having 3 columns.The respective length of each column I have mentioned below.
Column Name Data length
---------- ----------
A 1
B 2
C 2
D 8
E 8
current value of my trailer is
Column Name Data length
---------- ----------
A 1
B 2
C 15
sample records for header :-
1cc 12345 43434
I need to replace the above sample record second column value with yyy.i.e from cc to yyy.
if possible can you please tell me how to replace only the header & trailer of the file rest of the records need to be the same
---------- Post updated 08-03-13 at 12:48 AM ---------- Previous update was 08-02-13 at 07:17 PM ----------
sed 's/\(.\{1\}\)../\1yyy/'
This command is actually replacing the value of all the records in the file.But I need to replace the value of header & trailer only .Please some one help here
What's the difference between the 3 sections header,detail,trailer?
Could you give 3 example lines for each? (To find a contents criterion.)
Perhaps there are fixed line numbers, e.g. trailer starts at line 100 or is 10 lines long?
So, you will note that the output doesn't have an additional space character in the last line of your output; instead, it is missing five space characters. This change in the number of spaces at the end of the last line in your output was not caused by the sed command:
sed 's/\(.\{1\}\)../\1yyy/' Test123.txt
You must be doing something else that you haven't shown us that is making that change.
I don't know how I can make my requirement understand
HAR 1129201202:33:13
D3065507
TAR000000000000144
In the above example which I show you , I need to replace AR with YYY.
The below code will replace for all the three records, but I need to replace only for the first and last record .And also while replacing, it shouldn't add an extra space.It should remove one space and add that record Means Length should be only 24.
Note: -Its a fixed width file.All the records will have a length of 24
sed 's/\(.\{1\}\)../\1yyy/'
I created a file with your posted data. Two things to note here:
(1) I replaced all spaces by "*" so they are easily visible.
(2) The length of each line is 24. It wasn't so in your post, but I think that was due to copy/paste issues.
Here's my file:
$
$ cat f01
HAR**1129201202:33:13***
D3065507****************
TAR000000000000144******
$
$ # check length of each line
$ awk '{print length}' f01
24
24
24
$
$
Now you mention two things in your post -
(a) you want to replace all "AR"s by "yyy"
(b) you want to replace only first and last record (the head and tail record)
So, will "AR" be present only in the first and last record i.e. only in the head and tail record? Let's say it does. Then we can use "AR" to match the lines we want to change:
Here, "/^.AR/" means - "find out all lines that have any character at the beginning, followed by AR". This will work only on the first and last line. The second line will remain as it is.
The first operation "s/AR/yyy/" performs the required substitution.
The second operation "s/.$//" removes the last character in the line so that the length is constant. Note that it removes the last character - which could be a space or something else.
Now to the addressing. You still haven't said a word about the number of lines per section.
This substitutes in lines 1...1 and in lines 3...3 only (as you have given for header and trailer in your last example)