Replace carriage return with colon on specific column

Hi,

I have to process four source text files with possibility to contain carriage return on the particular column. Thus, i need to replace the carriage return with 3 colons <:::>

The particular column position in the source files is not fix but the name is fixed. That is, say for example, Remarks column

Sample source file 1:

col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10
A_1|1|2|3|4|5|6|Testing 
remarks|8|9

desire output:

col1|col2|col3|col4|col5|col6|col7|Remarks|col9|col10
A_1|1|2|3|4|5|6|Testing:::remarks|8|9

Sample source file 2:

col1|col2|col3|Remarks|col5|col6|col7
A_1|1|2|Another
Testing Remarks|4|5|6

desire output:

col1|col2|col3|Remarks|col5|col6|col7
A_1|1|2|Another:::Testing Remarks|4|5|6

Is the header always present at the beginning of said files?

yes, it is always in the first line of the source text file.

Try this:

~/unix.com$ awk -F\| 'FNR==1{n=NF}NF<n{l=$0;getline;$0=l":::"$0}1' sample sample2

If you want to redirect files in its own output file, try this:

~/unix.com$ awk -F\| 'FNR==1{f=FILENAME".REDIRECTED";n=NF}NF<n{l=$0;getline;$0=l":::"$0}{print>>f}' sample sample2

Executing it twice or more will append output on the sample*.REDIRECTED file. If you do not want that, you have to remove sample*.REDIRECTED files before executing the script again.

(Old code)

~/unix.com$ awk -F\| 'BEGIN{getline;n=NF;print}NF<n{l=$0;getline;$0=l":::"$0}1' sample
1 Like

thanks a lot tukuyomi, it works.