linuxunixawksed

Use blank line as file separator in awk


I need to know if its possible to use a new line as a field seperator in awk to bring multiple lines in a single line ?

for example:

$ cat yo  
a aa aaa  
bb bbb bbb  
cccc ccccc cccc  
ddd dddd ddd  
eeeee eeeee eee  

fffff ffffff fffffff  
gggg ggggg  
hhhhhh hhhhhhh hhhhhhhhh  

iii iiiiiiiii iiiii  
jjjj jjjjj jjjjj  
kkkkk kkkkk  
lllllllll lll ll  

Below are the few thing my little brain could think of,but none helped.

cat file |awk -F'\n' '{print}'    
cat yo |awk 'NF'  '{print $NF}'  


cat yo |awk -F'/^$/d'  '{print $NF}'  
cat yo |awk -F'^$^[ \t]*$' '{print $NF}'  

cat yo |awk -F'^..' '{print $NF}'  

cat yo |awk -F'\t' '{print}'  

desired output:

a aa aaa  bb bbb bbb cccc ccccc cccc ddd dddd ddd eeeee eeeee eee  
fffff ffffff fffffff gggg ggggg hhhhhh hhhhhhh hhhhhhhhh  
iii iiiiiiiii iiiii jjjj jjjjj jjjjj kkkkk kkkkk lllllllll lll ll  

Solution

  • You can define the record separator as RS=, which will make it paragraph-wise: every line is a field, every record is a block:

    $ awk -v RS= '{for (i=1; i<=NF; i++) printf "%s%s", $i, (i==NF?"\n":" ")}' file
    a aa aaa bb bbb bbb cccc ccccc cccc ddd dddd ddd eeeee eeeee eee 
    fffff ffffff fffffff gggg ggggg hhhhhh hhhhhhh hhhhhhhhh 
    iii iiiiiiiii iiiii jjjj jjjjj jjjjj kkkkk kkkkk lllllllll lll ll
    

    Which is in fact the same as:

    awk -v RS= '{for (i=1; i<=NF; i++) printf "%s%s", $i, (i==NF?ORS:FS)}' file