加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 使用awk按行数将行移动到文件中

发布时间:2020-12-14 01:06:03 所属栏目:Linux 来源:网络整理
导读:我有一个带有’|| o ||’的示例文件作为字段分隔符. www.google.org||o||srScSG2C5tg=||o||bngwqfarhansingla.it||o||4sQVj09gpls=||o||ngascash||o||||o||ms-bronze.com.br||o||||o|| 我想在1.txt中只移动一个字段,在not_1.txt中只移动一个字段.我使用以下
我有一个带有’|| o ||’的示例文件作为字段分隔符.

www.google.org||o||srScSG2C5tg=||o||bngwq
farhansingla.it||o||4sQVj09gpls=||o||
ngascash||o||||o||
ms-bronze.com.br||o||||o||

我想在1.txt中只移动一个字段,在not_1.txt中只移动一个字段.我使用以下命令:

sed  's/(||o||)+$//g' sample.txt  | awk -F '[|][|]o[|][|]' '{if (NF == 1) print > "1.txt"; else print > "not_1.txt" }'

问题是它不是原始线而是移动线.

我得到的输出是(not_1.txt):

td@the-end.org||o||srScSG2C5tg=||o||bnm
erba01@tiscali.it||o||4sQVj09gpls=

1.TXT:

ngas
ms-inside@bol.com.br

如您所见,原始线条已被修改.我不想修改这些行.
任何帮助将受到高度赞赏.

解决方法

Awk解决方案:

awk -F '[|][|]o[|][|]' 
'{ 
     c = 0; 
     for (i=1; i<=NF; i++) if ($i != "") c++;
     print > (c == 1? "1" : "not_1")".txt"  
 }' sample.txt

结果:

$head 1.txt not_1.txt 
==> 1.txt <==
ngascash||o||||o||
ms-bronze.com.br||o||||o||

==> not_1.txt <==
www.google.org||o||srScSG2C5tg=||o||bngwq
farhansingla.it||o||4sQVj09gpls=||o||

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读