I was wondering if there's a way to fix the line breaks between records and replace only single \n (newline) chars in a text file using awk or sed? Currently my code is replacing all the newlines with "|" .But I need it to only replace single newline characters and not double or multiple.
If each record is separated by pipes then I will be able to fix the line breaks, but i dont know how to do so.
This is my select query to get the extract :-
select a."rec1" || "|" || a."rec2" || "|" || a."rec3" || "|" || a."rec4" || "|" || a."rec5" || "|" || a."rec6" from tab_1 a
union
select select b."rec1" || "|" || b."rec2" || "|" || b."rec3" || "|" || b."rec4" || "|" || b."rec5" || "|" || a."rec6" from tab_2 b
union
select select c."rec1" || "|" || c."rec2" || "|" || c."rec3" || "|" || c."rec4" || "|" || c."rec5" || "|" || a."rec6" from tab_3 c
I have multiple select queries that I unionise to get the desired extract. I want to print "|" after each column record. Such that all the records of the column must be enclosed between pipes including the last column record. The desired output must look something like this :
desired extract output:
||rec 3|rec 4|rec 5|rec 6|
rec 1|rec 2|rec 3|rec 4|rec 5|rec 6|
|rec 2|rec 3|rec 4|rec 5|rec 6|
extract output :
||rec 3|rec 4|rec 5|rec 6
rec 1|rec 2|rec 3|rec 4|rec 5|rec 6
|rec 2|rec 3|rec 4|rec 5|rec 6
Note :- each column is already delimited with a | symbol. Hence if there's no record in that specific column as in the case of the first row there will be nothing displayed between pipes. Ideally if there are records present for each column the file would look something like this :
rec 1|rec 2|rec 3|rec 4|rec 5|rec 6
the desired output should be look like this :
rec 1|rec 2|rec 3|rec 4|rec 5|rec 6|
OR
|rec 1|rec 2|rec 3|rec 4|rec 5|rec 6
such that each record(if present) will be enclosed between pipes after removing newlines like this :-
||rec 3|rec 4|rec 5|rec 6|rec 1|rec 2|rec 3|rec 4|rec 5|rec 6||rec 2|rec 3|rec 4|rec 5|rec 6|
CodePudding user response:
Use this select query to get "|" between each column records.
select a."rec1" || "|" || a."rec2" || "|" || a."rec3" || "|" || a."rec4" || "|" || a."rec5" || "|" || a."rec6" || "|" from tab_1 a
union
select select b."rec1" || "|" || b."rec2" || "|" || b."rec3" || "|" || b."rec4" || "|" || b."rec5" || "|" || a."rec6" || "|" from tab_2 b
union
select select c."rec1" || "|" || c."rec2" || "|" || c."rec3" || "|" || c."rec4" || "|" || c."rec5" || "|" || a."rec6" || "|" from tab_3 c
Once you get the extract you can remove the newlines & pipes using below awk & tr
awk -F'\n' -v RS= -v OFS='' '{$1=$1;printf "%s", $0 RT}' extract.txt | tr '|' '\n' | awk 'NF >0' extract.txt > final_extract.txt