sed delete char, not shift left? - TechRepublic
General discussion
May 11, 2006 at 06:39 AM
jeffreymangan

sed delete char, not shift left?

by jeffreymangan . Updated 19 years, 10 months ago

Example: There is a flat file on Unix that contains these 2 lines of text. They are formatted to have specific field start position that will be read by the program.

{Start of file}
Joe Smith Tampa Florida
Erin Jones Seattle Washington
{End of file}

Name: is in field position 1-14
City: is in field position 16-26
State:is in field position 28-42

Consider this… If I edit the file and insert Jr. to the end of Joe Smith it will shift City and State 2 positions to the right. This will cause the program to fail because the format changed. In this simple example I would then backspace twice to bring the city and state fields back into position.

But here is my actual problem.
The file will arrive with a cntrl-char in the text. To clean these files I can run a shell script to either replace with blank/ / or to delete// the ^Z. I want to delete the ^Z, which works but then city and state shift one space to the left. How do I delete the ^Z but at the same time add one space(s) to the end of the word to maintain it’s field position?

sed -e ‘s/^Z//g’ DIRTYFILE > /opt/yantra/wmsp/CLEANFILE

{Start of file}
Joe Smith Tam^Zpa Florida
Erin Jones Seattle Washington
{End of file}

In this example if I use sed to delete ^Z I would end up with Florida out of position. I need to delete the ^Z from Tampa but then add a space or insert after Tampa to move Florida one space to the right.

{Start of file}
Joe Smith Tampa Florida
Erin Jones Seattle Washington
{End of file}

This discussion is locked

All Comments