将每n行转换为一行

时间:2015-04-24 07:35:16

标签: linux shell awk sed paste

我想把一堆n行放到一行,不知道怎么做。任何帮助表示赞赏。

一堆11个单词然后一个空白行然后再一堆ll单词然后空白像......等等。

实施例

cat filename

hi
hello
how
are
you
i
am
fine
how
are
you

hi
how
is
she
doing
i 
have
not 
herd 
from
her

..
..

期望的输出:

cat newFile

hi hello how are you i am fine how are you 
hi how is she doing i have not heard from her 
..
..

5 个答案:

答案 0 :(得分:2)

将由空行分隔的文本块作为单个记录处理,在awk中称为段落模式:

$ awk -v RS= '{$1=$1}1' file
hi hello how are you i am fine how are you
hi how is she doing i have not herd from her

答案 1 :(得分:1)

通过awk。

$ awk -v RS= '{gsub(/\n/, " ")}1' file
hi hello how are you i am fine how are you
hi how is she doing i  have not  herd  from her

答案 2 :(得分:0)

你也可以这样做(虽然有点长):

$(document).ready(function(){

    /* jQuery Droppable */
    $(function() {
        $( ".mn-items .rp-draggable li" ).draggable({
            appendTo: "body",
            helper: "clone"
        });
        $( ".header-favorites ul" ).droppable({
            activeClass: "ui-state-default",
            hoverClass: "ui-state-hover",
            accept: ":not(.ui-sortable-helper)",
            drop: function( event, ui ) {
                $( this ).find( ".placeholder" ).remove();
                $(ui.draggable).clone().appendTo(this);
            }
        }).sortable({
            items: "li:not(.placeholder)",
            sort: function() {
                $( this ).removeClass( "ui-state-default" );
            }
        });
    });


    /* Click Star Icon to Add to Drop Here Container */
    $('ul.rp-draggable li .fa-star-o').click(function(){
        $(this).closest('li').clone().appendTo('.h-droped-list');
        $(this).closest('li').toggleClass('addedToFav');
    });

    /* Click Close Icon to Remove from Drop Here Container */
    $("ul.h-droped-list li .fa-star-o").click(function(){
        $(this).closest('li').remove();
    });

});

Ouptput:

#!/bin/bash
c=0
while read -r i
do
if [[ $i == "" ]]; then
line="$line""\n"
c=2
elif [[ $c == "2" || $c == "0" ]]; then
line="$line""$i"
c=1
else
line="$line"" ""$i"
fi
done <file1
echo -e "$line"

答案 3 :(得分:0)

如果您需要在脚本中使用已组装的行,则可以在解析文件时将每行存储在数组中以供以后使用:

#!/bin/bash

declare -i cnt=0
declare -a array

while read -r ln; do                            # read each line as ln
    [ "$ln" == "" ] && {                        # test for empty ln
        [ $cnt -gt 0 ] && {                     # if not first empty line
            while [ "${line:0:1}" == ' ' ]; do  # trim leading spaces from line
                line="${line:1}"
            done
            array+=( "$line" )                  # add line to array
            unset line                          # unset line
            continue                            # get next line
        }
        ((cnt++))                               # increment count
    }
    line+=" $ln"                                # append ln to line
done <"$1"
[ ${#line} -gt 0 ] && array+=( "$line" )        # if no trailing blank line

for ((a = 0; a < ${#array[@]}; a++)); do        # output array
    printf " array[%d]: %s\n" "$a" "${array[a]}"
done

exit 0

<强>输入

$ cat dat/bunchw.txt

hi
hello
how
are
you
i
am
fine
how
are
you

hi
how
is
she
doing
i
have
not
herd
from
her

<强>输出

$ bash rdwordsline.sh dat/bunchw.txt
 array[0]: hi hello how are you i am fine how are you
 array[1]: hi how is she doing i have not herd from her

答案 4 :(得分:0)

所需的输出,但每11个元素转置一次。

awk 'ORS=NR%11?FS:RS'