update specific field in text file in specific line

后端 未结 2 659
礼貌的吻别
礼貌的吻别 2021-01-17 02:42

Below is a code that I\'m able to update the quantity of my book. But I cant seem to make it so that I can update my other things like book name, author, price etc. Heres th

2条回答
  •  抹茶落季
    2021-01-17 03:07

    What about sed?

    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
    

    Send the output with redirection > to a new file or use sed's -i option for in place editing.

    • redirection

      sed 's/\bLord of The Ring\b/Lord of The King/' file>newfile
      
    • in place etit with .bak file

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

提交回复
热议问题