General discussion

Locked

sed delete char, not shift left?

By JeffreyMangan ·
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 conversation is currently closed to new comments.

4 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by cpfeiffe In reply to sed delete char, not shif ...

Substitute the ^Z with two spaces.

sed 's/^Z/ /g' DIRTYFILE > /opt/yantra/wmsp/CLEANFILE

You can use the '-e' option on sed if you want, but it isn't required for only one edit.

Collapse -

by JeffreyMangan In reply to sed delete char, not shif ...

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

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

If I replace ^Z with one space then the city will be "Tam pa" which will keep the format but that won't be a valid city.
If I replace ^Z with two spaces then the city will be "Tam pa" which will shift the entire line to the right and will make the field positions wrong and will make the city invalid.

I want to remove the ^Z from Tam^Zpa with // so it will make "Tampa" with no space but this also shifts the line to the left. I need to replace ^Z with // but then need to add a space to the end of "Tampa" so it moves the line to the right to make up for the missing ^Z.

Collapse -

by Martin Simons In reply to sed delete char, not shif ...

So substitute Ctrl Z and anything but a space up until the next space with those same cahracters and then a space as follows:

sed 's/^Z\([^ ]*)/\1 /g' DIRTYFILE > /opt/yantra/wmsp/CLEANFILE

Collapse -

by Martin Simons In reply to

Sorry, you need a backslant \ before the closing paren ).

Back to Linux Forum
4 total posts (Page 1 of 1)  

Related Discussions

Related Forums