简体   繁体   English

使用Bash脚本编辑文本文件

[英]Edit a text file using a Bash script

I have file which is of the following format: 我有以下格式的文件:

axel localhost> localhost>
mike localhost> STORAGE 298390/512000 (58.279296875%) localhost>
ab localhost> STORAGE 141361/512000 (27.6095703125%) localhost>
fghi localhost> localhost>
abcd localhost> STORAGE 165929/512000 (32.4080078125%) localhost>
lmno localhost> STORAGE 353073/512000 (68.9595703125%) localhost>
nuon localhost> localhost>
spar localhost> STORAGE 203766/512000 (39.798046875%) localhost>
fibo localhost> STORAGE 238204/512000 (46.52421875%) localhost>
nacci localhost> STORAGE 425737/512000 (83.1517578125%) localhost>
seldom localhost> STORAGE 69894/512000 (13.651171875%) localhost>
project localhost> STORAGE 86220/512000 (16.83984375%) localhost>
eccleston localhost> STORAGE 240084/512000 (46.89140625%) localhost>
swan localhost> STORAGE 279522/512000 (54.594140625%) localhost>

This is the output generated by listquota in cyradm (Cyrus-imapd). 这是通过所产生的输出listquota在cyradm(赛勒斯-IMAPD)。 I have two questions; 我有两个问题;

  • In those lines which have localhost> localhost> , the first word should be written into another file and that line should be deleted. 在那些具有localhost> localhost> ,应将第一个单词写入另一个文件,并删除该行。
  • For the other lines, the second, third and last columns need to be deleted. 对于其他行,需要删除第二列,第三列和最后一列。

How would I go about achieving this result? 我将如何实现这一结果?

Edit 编辑

Expected output 预期产出

mike 298390/512000 (58.279296875%)
ab 141361/512000 (27.6095703125%)
abcd 165929/512000 (32.4080078125%)
lmno 353073/512000 (68.9595703125%)
spar 203766/512000 (39.798046875%)
fibo 238204/512000 (46.52421875%)
nacci STORAGE 425737/512000 (83.1517578125%)

My loop so far looks something like: 到目前为止我的循环看起来像:

while read thisline; do
    if [ $2 == "localhost>" AND $3 == "localhost>" ];then
        echo $1 >>/root/names.txt
        sed '1d' filename.txt #but this deletes only the first line
    fi

done

try this: 尝试这个:

awk '/localhost> localhost>/{print $1 >"file2";next}{$2=$3=$NF=""}7' file

this will output text you want, and generate a "file2" for the localhost> localhost> case. 这将输出您想要的文本,并为localhost> localhost> case生成“file2”。

didn't test, I hope there is no typoes. 没试过,我希望没有错字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM