Andres
Andres

Reputation: 275

Get into a line from a text file and edit it

Supposedly I have this line in my text file which has the following format.

"Title:Author:Price:QtyAvailable:QtySold"

The contents of my text file is shown below

 Hello World:Andreas:10.50:10:5
 Lord Of The Rings:Duke:50.15:50:20
    (some other records...)

1) User inputs Author and Title.

2) If Program locates the Author+Title, it asks user to Update any of the fields available ( for this case Title, Author, Price etc..

3) For example I will like to update the Price of Hello World book.

4) What can I do to tell the program to extract the contents of the Hello World line, and get into 10.50 to replace the price of the book? (assumes new price of the book will be decided by the user's input)

Hope to get my answer. Thanks in advance to those who helped!

Upvotes: 1

Views: 276

Answers (2)

anubhava
anubhava

Reputation: 784898

You can use sed like this with variable parameters:

# book title:author to be searched
BOOK="Hello World:Andreas"

# price to be updated
PRICE=11.25

# search and update with original backed up with .bak extension
sed -i.bak 's/\('"$BOOK"'\):[^:]*:/\1:'"$PRICE"':/' file.txt

Explanation:

If you fill-in shell variables into sed command it will look like this:

sed 's/\(Hello World:Andreas\):[^:]*:/\1:11.25:/'

match

"$BOOK"                 # match literal text i.e. Hello World:Andreas
\($BOOK\)               # group this text to be back referenced later
:                       # match literal :
[^:]*                   # match 0 or more characters until : is found
:                       # match a :

replacement

\1                      # group # 1 i.e. Hello World:Andreas
:                       # a literal :
$PRICE                  # fill in the new price
:                       # literal :                   

Essentially this sed command is finding text that has Hello World:Andreas: followed by some price value and then followed by another :. Once this patter is found it is replacing that with back-reference # 1 (which is Hello World:Andreas) followed by a : and then put the new price value and a colon :.

EDIT: You are highly recommended to read some sed tutorial however as per your comment I am providing you command to update quantity:

# book title:author to be searched
BOOK="Hello World:Andreas"

# quantity to be updated
QTY=18

# search and update with original backed up with .bak extension
sed 's/^\('"$BOOK"'\):\([^:]*\):[^"]*:/\1:\2:'"$QTY"':/'

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77075

Following is a something to get you started:

Sample Script:

[jaypal:~/Temp] cat s.sh 
#!/bin/bash

echo "Author?"
read author

echo "Title?"
read title

grep -c "$title:$author" file > /dev/null # Look for a line with matching values

if [ $? == 0 ]; then # If found then offer to change price
    echo "I found the book, Do you want to update price to what?"
    read newprice
    sed -i "s/\($book:$author\):[^:]*:/\1:$newprice:/" file
fi

Input Data:

[jaypal:~/Temp] cat file
Hello World:Andreas:10.50:10:5
Lord Of The Rings:Duke:50.15:50:20

Execution:

[jaypal:~/Temp] ./s.sh 
Author? 
Andreas
Title? 
Hello World
I found the book, Do you want to update price to what?
40
[jaypal:~/Temp] cat file
Hello World:Andreas:40:10:5
Lord Of The Rings:Duke:50.15:50:20

Upvotes: 0

Related Questions