LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2018, 12:10 PM   #1
Bensonl92
LQ Newbie
 
Registered: Jan 2018
Posts: 2

Rep: Reputation: Disabled
Unhappy add/subject values from field based on userinput using awk


so basically im doing a program for school regarding a book inventory system. and im stucked on this part where the user is allowed to update sold books.

@echo "Enter title"
read $title
@echo "Enter author"
read $author
@echo "Enter copies sold"
read $sold

awk -v x="$title" -v y="$author" 'BEGIN{FS=OFS=":"}{if (($1~x) && ($2~y)) print $5-$sold}' BookDB.txt

storing format is [Title]:[Author]:[Price]:[Stock]:[Sold]
so basically i need to minus stock and increase sold

any help will be good
 
Old 01-09-2018, 07:39 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,606

Rep: Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935
Welcome to LQ

Please use [code][/code] tags around your code to maintain format and readability.

1. I presume the awk is inside another script? What language is it?

2. Are you allowing the user to only enter part of the author and/or title? If so, your solution will currently output multiple entries.

3. $sold needs to be passed to awk the same as $title and $author

4. Used in this way, awk will not update anything and simply output information

5. You subtract $sold from the fifth field, but this is the Sold field ... shouldn't this be an addition? (as per the end of your post)
 
Old 01-09-2018, 07:39 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,208

Rep: Reputation: 705Reputation: 705Reputation: 705Reputation: 705Reputation: 705Reputation: 705Reputation: 705
Code:
awk -v x="$title" -v y="$author" 'BEGIN{IGNORECASE = 1;OFS=FS=":"} ($1 ~ x && $2 ~ y){$4--;$5++} {print}' BookDB.txt > BookDB.txt.new
IGNORECASE is gawk feature (ignore case in regexp)

Sorry did not see the copies sold variable.
You have to pass this variable with -v, and do the math with $5 and $4

As grail said, it's easy to make error here and update pseudo duplicated lines, ideally each book should have an uniq ID

Last edited by keefaz; 01-09-2018 at 07:46 PM.
 
Old 01-09-2018, 07:52 PM   #4
Bensonl92
LQ Newbie
 
Registered: Jan 2018
Posts: 2

Original Poster
Rep: Reputation: Disabled
Oh yeah sorry the sold is +.
and i got it to work already.
i output S5 out to a variable, and then calculate the new values before passing it back in to save.

VAR1=$(awk -v x="$title" -v y="$author" -F ":" '($1 ~ x && $2 ~ y) {print $5}' BookDB.txt | tr -d "[]")

updatedsold=$(expr $VAR1 + $update)

awk -v x="$title" -v y="$author" 'BEGIN{FS=OFS=":"}{if (($1~x) && ($2~y)) $5="["'$updatedsold'"]"}1' BookDB.txt > tmpBookDB.txt && mv tmpBookDB.txt BookDB.txt

now i need to find a way to shorten it hahaha
 
Old 01-09-2018, 11:53 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,606

Rep: Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935
Depending on your version awk (gawk) you can change the file in place. You also could ask all your questions and do all the steps in a single awk script, ie. instead of whichever shell you are using.

You also didn't answer the question about uniqueness? Perhaps you could show an example of the input file and what the user might enter?

I have also just noticed that the square brackets are actually part of the input to (thought they were just place holders).
You could place the square brackets in the FS variable if it makes it easier?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to match different records based on Same Field values using awk Abdelrahman.fathy Programming 22 11-11-2015 10:32 PM
[SOLVED] How to match different records based on multiple Field values using awk Abdelrahman.fathy Programming 4 11-04-2015 10:49 PM
Equivalence classes, based on field values and multi-key hashtable openSauce Programming 5 05-16-2010 08:35 PM
Add values using awk Helene Programming 4 04-21-2004 06:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration