sedlf

Removing line feeds from file is not working


I trying to use sed to remove the linefeed from lines that contain a pattern. My lines are:

              Format:                    0000000
              InputMask:                 &&&&&&&
              OrdinalPosition:           0

              Required:                  True
              SourceField:               LOAN-NO
              SourceTable:               MASTER

I want it to look like this:

              Format:                    0000000
              InputMask:                 &&&&&&&
              OrdinalPosition:           0

              Required:                  True
              SourceField:               LOAN-NO:SourceTable:               MASTER

My code is:

cat file.txt | sed 's/\(*.SourceField.*\)\(\n\)/\1:/g'

The code doesn't change anything. In fact even when I remove the pattern and look for \n it still doesn't work

This seems to work for all lines:

cat file.txt | sed ':a;N;$!ba;s/\n//g' 

but I cant get it to work for just the lines containing with SourceField.

I'd like to understand why the first line doesn't work, and how the second line can be adapted to work with just the lines containing the pattern.

Any help would be appreciated.

Thanks!


Solution

  • $ sed -E '/SourceField:/{N;s/\n */:/;}' file
    
                  Format:                    0000000
                  InputMask:                 &&&&&&&
                  OrdinalPosition:           0
    
                  Required:                  True
                  SourceField:               LOAN-NO:SourceTable:               MASTER
    

    your first script won't work because sed operations are line based. In the pattern space you can remove the new line.