Search code examples
awksedreplacesplitcut

AWK replace a big string with a part of it in one column (cut a part of it), and print all other columns intact, keeping the formatting


I want to replace a big string with a part of it - text from the second slash "/" in the column 2. Also the formatting should be maintained.

My text is

99-AAA-9999 |  ZZZ/AAA/999999/AAA/99.99.9999                        |            |       9.99 |   99999.99 |
99-AAA-9999 |  ZZZ/AAAA AAAA/999999/AAA/99.99.9999                  |            |       9.99 |    9999.99 |
99-AAA-9999 |  ZZZ/AA AAAAAAA/999999/AAA/99.99.9999                 |            |       9.99 |   99999.99 |
99-AAA-9999 |  ZZZ/AAA AAAAA AAAAAAA AAAAAA/999999/AAA/99.99.9999   |            |       9.99 |   99999.99 |
99-AAA-9999 |  ZZZ/AAAA AAAAAA AAAAA/999999/AAA/99.99.9999          |            |       9.99 |   99999.99 |

I want it like this after cutting off the text from ZZZ/AA..... upto the second slash "/" with good formatting

99-AAA-9999 |  999999/AAA/99.99.9999 |            |       9.99 |   99999.99 |
99-AAA-9999 |  999999/AAA/99.99.9999 |            |       9.99 |    9999.99 |
99-AAA-9999 |  999999/AAA/99.99.9999 |            |       9.99 |   99999.99 |
99-AAA-9999 |  999999/AAA/99.99.9999 |            |       9.99 |   99999.99 |
99-AAA-9999 |  999999/AAA/99.99.9999 |            |       9.99 |   99999.99 |

Actually there are many other columns, but I have provided only sample data. Note : All the text and numbers are not the same.

Please guide me how to do it using awk or sed, preferably awk.


Solution

  • $ sed 's:[^ /]*/[^/]*/\([^|]*[^ |]\) *|:\1 |:' file
    99-AAA-9999 |  999999/AAA/99.99.9999 |            |       9.99 |   99999.99 |
    99-AAA-9999 |  999999/AAA/99.99.9999 |            |       9.99 |    9999.99 |
    99-AAA-9999 |  999999/AAA/99.99.9999 |            |       9.99 |   99999.99 |
    99-AAA-9999 |  999999/AAA/99.99.9999 |            |       9.99 |   99999.99 |
    99-AAA-9999 |  999999/AAA/99.99.9999 |            |       9.99 |   99999.99 |