I have a flat file containing columns of data representing fields from a table. I get the file from an outside source and want to insert the data into a table in my database. Unfortunately, the file I receive is missing a field in a specific column (new field that was added). Since I have no control of what is being sent to me, I would like to edit the file and just add the field. Can sed
be used to add text in a specific column on every row?
For example, let's say I have this file:
Alan Bradford 555-2012
Cathy Davies 555-7823
Edward Farris 555-9162
Gary Hobbs 555-5151
Irene Jacobs 555-1285
The file is missing the area codes, so I want to add 213
in front. I know the phone numbers always start in column (pure character count) 31. So I want it to show
Alan Bradford 213 555-2012
Cathy Davies 213 555-7823
Edward Farris 213 555-9162
Gary Hobbs 213 555-5151
Irene Jacobs 213 555-1285
I know I can do this in three passes. I can use cut -c1-30
and get Part1, and cut -c31-
to get Part2. Then I can paste it all together with echo "$Part1 $NEWDATA $Part2" >> filename
I just wonder if there is a much easier way using sed. I should be able to use something like
sed -e "30l,i213 " InFile > OutFile
I just can't seem to get the syntax right to say move over 30 characters on the line, then insert 213
.
Anyone know what might work, or work better than my cut and paste options?
UPDATE
I'm told that my example was not accurate enough and I should edit the question to stop wasting people's time. The example I gave was pretty accurate for the question: How do I insert a string "XXX" ALWAYS in position Y no matter what is before or after it?
But no problem...Here's my real world example. I have a text file with rows of 928 characters each. I want to insert a string starting at position 878. The values before and after the string cannot be counted on to be the same each time, since the next field after where I want to insert is a remarks field, which is usually but not always blank.
The answer from @DonHolgo had the most promise and is a great answer. But on my flavor of UNIX (AIX 7.1) it seems that it only allows you to keep track of up to 255 characters before you get an error.
Here I am inserting "XXX " at column 255:
# sed 's/.\{255\}/&XXX /' OrigTextFile
1 030680001001YNPO 14 H502 000595000000000000 1 0000680M00000100000004799000000000000479900000004799000000004799000000000000479900000 SDI42028820 20P561292 00000000000XXX 000000000000000000000000000000000000000000000000000000000000000 T 0000655000000000Y 0000516000000000E 0000280000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 20200814
But I try the same thing for inserting at column 256 or higher:
# sed 's/.\{256\}/&XXX /' OrigTextFile
sed: 0602-404 Function s/.\{256\}/&XXX / cannot be parsed.
Looks like a limitation on my version of sed. So I may have to just do my original plan of cutting the textfile up. File1 = First 878 characters of each line from the textfile File2 = New string I want to add, one line for each line in original file File3 = Remaining characters from original textfile.
Then just join them all:
# paste File1 File2 File3 > NewTextFile
NNN-NNNN
?