更新特定行中文本文件中的特定字段

时间:2013-07-27 11:28:08

标签: linux shell unix sed

以下是我可以更新图书数量的代码。但我似乎无法做到这一点,以便我可以更新我的其他东西,如书名,作者,价格等。 下面是代码:

if grep -q "^$bookname:$author:" BookDB.txt
   then 
   read -p "Update Qty to what?" newQty 
   sed -i "s/^\($bookname:$author:[^:]*\):[^:]*:/\1:$newQty:/" BookDB.txt 
   echo "Book's Qty has been updated successfully!" 
   else 
   echo "$0: BookDB.Txt: no '$title' by '$author'" >&2 
   fi

我的数据库是这样的:

Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The Ring:Johnny Dept:56.80:100:38
Three Little Pig:Andrew Lim:89.10:290:189
All About Ubuntu:Ubuntu Team:76.00:55:133
Catch Me If You Can:Mary Ann:23.60:6:2
Happy Day:Mary Ann:12.99:197:101
haha:gaga:1:10:1

那么我怎么能改变某些领域,比如当我选择书名时,戒指作者约翰尼·德普的主我可以将书名改为国王的主人?

Lord of The King:Johnny Dept:56.80:100:38

2 个答案:

答案 0 :(得分:1)

user1146332 一样,我也建议切换到更强大的语言。这是一个使用的示例。它使用模块Getopt::Long来读取参数并在文件中循环以获得匹配的行。

它似乎有效,但是你可以更好地检查错误,例如,在正则表达式中使用它们之前定义bookauthor变量。

还有一件事,我使用带有冒号的split()来提取字段,我不知道你如何处理名字中带有冒号的书,但在这种情况下,你需要一个额外的模块来解析CSV ,就像Text::CSV一样,或者用更好的split表达来更加努力。

#!/usr/bin/env perl

use strict;
use warnings;
use Getopt::Long;

my ($book, $author, $newbook, $quantity);

my $r = GetOptions(
        q|book=s| => \$book,
        q|author=s| => \$author,
        q|newbook=s| => \$newbook,
        q|quantity=i| => \$quantity,
) or die;

open my $fh, '<', $ARGV[0] or die;

while ( <$fh> ) { 
        chomp;
        my @f = split /:/;
        next if @f < 5;
        if ( $f[0] =~ m/(?i)\Q$book\E/ &&
             $f[1] =~ m/(?i)\Q$author\E/ ) { 
                $f[0] = defined $newbook ? $newbook : $f[0];
                $f[3] = defined $quantity ? $quantity : $f[3];
                printf qq|%s\n|, join q|:|, @f; 
                next;
        }   

        printf qq|%s\n|, $_; 
}

一个例子:

perl script.pl --book="lord of the ring" --author=dep --newbook="Lord of the King" --quantity=99 infile

收率:

Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of the King:Johnny Dept:56.80:99:38
Three Little Pig:Andrew Lim:89.10:290:189
All About Ubuntu:Ubuntu Team:76.00:55:133
Catch Me If You Can:Mary Ann:23.60:6:2
Happy Day:Mary Ann:12.99:197:101
haha:gaga:1:10:1

答案 1 :(得分:1)

怎么样?

sed 's/\bLord of The Ring\b/Lord of The King/' file

$ cat file
Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The Ring:Johnny Dept:56.80:100:38
Three Little Pig:Andrew Lim:89.10:290:189
All About Ubuntu:Ubuntu Team:76.00:55:133
Catch Me If You Can:Mary Ann:23.60:6:2
Happy Day:Mary Ann:12.99:197:101
haha:gaga:1:10:1

$ sed 's/\bLord of The Ring\b/Lord of The King/' file
Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The King:Johnny Dept:56.80:100:38
Three Little Pig:Andrew Lim:89.10:290:189
All About Ubuntu:Ubuntu Team:76.00:55:133
Catch Me If You Can:Mary Ann:23.60:6:2
Happy Day:Mary Ann:12.99:197:101
haha:gaga:1:10:1

将带有重定向>的输出发送到新文件,或使用sed的-i选项进行就地编辑

  • 重定向

    sed 's/\bLord of The Ring\b/Lord of The King/' file>newfile
    
  • 使用.bak文件

    的地方etit
    sed -i.bak 's/\bLord of The Ring\b/Lord of The King/' file