Regex - Write values in single lines

Viewed 47

I have xml file where structure looks as below:

<Column
             sourceColumn="Column1"
             targetColumn="COLUMN1"
/>
<Column
             sourceColumn="Column2"
             targetColumn=
/>

Maybe is simple solution how I can get in result in form:

sourceColumn="Column1"spaceOr_targetColumn="COLUMN1"
sourceColumn="Column2"spaceOr_targetColumn=

Someone can me help? I tried

cat file.xml | grep "sourceColumn=\|targetColumn=" |tr '\n' ' ' | sed 's/sourceColumn=/\nsourceColumn=/g' >> file1.txt 

but when I open file1.txt I have structure:

enter image description here

1 Answers

With your shown samples, please try following awk program. Written and tested in GNU awk.

awk -v RS='\n*<Column\n*|\n*/>\n*' '
NF{
  gsub(/^[[:space:]]+|\n*/,"")
  sub(/ +/,"spaceOr_")
  print
}
' Input_file

Explanation: Simply making RS(record separator) as \n*<Column\n* OR \n*/>\n* for all lines of Input_file. Then in main program checking if line is not null then remove initial spaces or ending lines. Then substituting spaces with spaceor_ string, finally printing the value.

Related