您好,登錄后才能下訂單哦!
這篇文章主要介紹awk如何將多行文件轉換為一行,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
一、需求:
把這些數據庫文件轉換成一行,然后用scp -r 一次性作拷貝。
二、實例
[root@mysql5 ~]# cat /tmp/1 ga_10122 ga_10589 ga_11053 ga_11238 ga_11559 ga_11944 ga_12193 ga_13542 ga_35179 ga_36013 ga_36063 ga_36575 ga_36737 ga_36926 ga_37962 ga_39596 ga_39599 ga_39603 ga_39604 ga_39605 ga_39608 ga_39609 ga_39610 ga_39614 ga_39618 ga_39619 ga_39622 ga_39625 ga_39626 ga_39630 ga_39633 ga_39636 ga_39639 ga_39640 ga_39642 ga_39645 ga_39648 ga_39652 ga_39654 ga_39657 ga_39668 ga_39675 ga_39687 ga_39690 ga_39691 ga_39693 ga_39695 ga_39697 ga_39702 ga_39703 ga_39704 ga_39708 ga_39715 ga_39717 ga_39724 ga_39725 ga_39727 ga_39728 ga_39730 ga_39732 ga_39738 ga_39741 ga_39743 ga_39753 ga_39757 ga_39761 ga_39765
三、解決有三種
1、采用awk
[root@mysql5 ~]# awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}' /tmp/1 ga_10122 ga_10589 ga_11053 ga_11238 ga_11559 ga_11944 ga_12193 ga_13542 ga_35179 ga_36013 ga_36063 ga_36575 ga_36737 ga_36926 ga_37962 ga_39596 ga_39599 ga_39603 ga_39604 ga_39605 ga_39608 ga_39609 ga_39610 ga_39614 ga_39618 ga_39619 ga_39622 ga_39625 ga_39626 ga_39630 ga_39633 ga_39636 ga_39639 ga_39640 ga_39642 ga_39645 ga_39648 ga_39652 ga_39654 ga_39657 ga_39668 ga_39675 ga_39687 ga_39690 ga_39691 ga_39693 ga_39695 ga_39697 ga_39702 ga_39703 ga_39704 ga_39708 ga_39715 ga_39717 ga_39724 ga_39725 ga_39727 ga_39728 ga_39730 ga_39732 ga_39738 ga_39741 ga_39743 ga_39753 ga_39757 ga_39761 ga_39765
awk默認將記錄分隔符(record separator即RS)設置為\n,此行代碼將RS設置為EOF(文件結束),也就是把文件視為一個記錄,然后通過gsub函數將\n替換成空格,最后輸出。
2、采用sed
[root@mysql5 ~]# sed ':a ; N;s/\n/ / ; t a ; ' /tmp/1 ga_10122 ga_10589 ga_11053 ga_11238 ga_11559 ga_11944 ga_12193 ga_13542 ga_35179 ga_36013 ga_36063 ga_36575 ga_36737 ga_36926 ga_37962 ga_39596 ga_39599 ga_39603 ga_39604 ga_39605 ga_39608 ga_39609 ga_39610 ga_39614 ga_39618 ga_39619 ga_39622 ga_39625 ga_39626 ga_39630 ga_39633 ga_39636 ga_39639 ga_39640 ga_39642 ga_39645 ga_39648 ga_39652 ga_39654 ga_39657 ga_39668 ga_39675 ga_39687 ga_39690 ga_39691 ga_39693 ga_39695 ga_39697 ga_39702 ga_39703 ga_39704 ga_39708 ga_39715 ga_39717 ga_39724 ga_39725 ga_39727 ga_39728 ga_39730 ga_39732 ga_39738 ga_39741 ga_39743 ga_39753 ga_39757 ga_39761 ga_39765
說明:sed默認只按行處理,N可以讓其讀入下一行,再對\n進行替換,這樣就可以將兩行并做一行。但是怎么將所有行并作一行呢?可以采用sed的跳轉功能。:a 在代碼開始處設置一個標記a,在代碼執行到結尾處時利用跳轉命令t a重新跳轉到標號a處,重新執行代碼,這樣就可以遞歸的將所有行合并成一行。
3、cat file | xargs
[root@mysql5 ~]# cat /tmp/1 | xargs ga_10122 ga_10589 ga_11053 ga_11238 ga_11559 ga_11944 ga_12193 ga_13542 ga_35179 ga_36013 ga_36063 ga_36575 ga_36737 ga_36926 ga_37962 ga_39596 ga_39599 ga_39603 ga_39604 ga_39605 ga_39608 ga_39609 ga_39610 ga_39614 ga_39618 ga_39619 ga_39622 ga_39625 ga_39626 ga_39630 ga_39633 ga_39636 ga_39639 ga_39640 ga_39642 ga_39645 ga_39648 ga_39652 ga_39654 ga_39657 ga_39668 ga_39675 ga_39687 ga_39690 ga_39691 ga_39693 ga_39695 ga_39697 ga_39702 ga_39703 ga_39704 ga_39708 ga_39715 ga_39717 ga_39724 ga_39725 ga_39727 ga_39728 ga_39730 ga_39732 ga_39738 ga_39741 ga_39743 ga_39753 ga_39757 ga_39761 ga_39765
以上是“awk如何將多行文件轉換為一行”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。