LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-26-2010, 07:19 PM   #1
szahmad1
LQ Newbie
 
Registered: Nov 2009
Posts: 14

Rep: Reputation: 1
Question Variables


I was wondering if there is a way to change the value of positional parameters inside a file.I mean, i want to change the value of a certain field of a file to the one i provide with?
 
Old 03-26-2010, 08:45 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i think this is more programming but you probably want to look into awk
possibly cut, grep, sed, paste, ... would also be helpful.
 
Old 03-26-2010, 10:11 PM   #3
szahmad1
LQ Newbie
 
Registered: Nov 2009
Posts: 14

Original Poster
Rep: Reputation: 1
how to do it with awk? i am trying to serach a file on a certain field.when i find it i have to number those records using "for" loop,inside the awk command.
This is my file
A100:Annie Marie's Dance Academy:123:1500
F110:Frankies Repair Shop:1123:250
S100:Sandra's Counselling Service:67:500
A100:Annie Marie's Dance Academy:254:500

I need the output in a report form like this:
Vendor Name: Annie Marie's Dance Academy

1. Invoice #123 Amount: 1500
2. Invoice #254 Amount: 500

Here are the instructions:
It is recommended to use temporary files when matching valid vendor names to display the invoice and purchase amount information. You can then use command substitution and the set command to store vendor purchase information as positional parameters, and then use the for loop to display (hence number) each line...

Last edited by szahmad1; 03-26-2010 at 10:28 PM.
 
Old 03-26-2010, 11:06 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
I don't fully understand whether you want to CHANGE the values in the file, or SHOW the values from the file. Also, you already learned in another thread about this same exact subject, how to number the output lines using the NR variable from within `awk`. That thread is here: http://www.linuxquestions.org/questi...d.php?t=797911

Here's a way to SHOW the values; if you need to actually change them within the file, you'll want to use `sed` most likely.

Assuming the file you showed above is named "filename":
Code:
vendor="Annie Marie's Dance Academy"
echo -e "Vendor Name: $vendor\n$(grep -E "$vendor" filename | awk -F: '{print NR". Invoice #"$3" Amount: "$4}')"
So, that just shows the entries for this vendor, which are inside the <filename>. To change the file, or put the results in a new file, I will leave that to you.

Also, I'll see about moving this thread to /programming, and/or joining it with its long lost friend, linked above

Sasha
 
Old 03-26-2010, 11:57 PM   #5
szahmad1
LQ Newbie
 
Registered: Nov 2009
Posts: 14

Original Poster
Rep: Reputation: 1
My previuos posts were little confusing.Actually i wanna search the file based on the first field in this case "A100" which has 2 entries in this file.Then i want to output a report which gives the name name of the vendor once and the numbered list for the transactions associated with it.Presently i am searching for the specific entries, putting it in a temporary file and then displaying the report for that specific entries. e.g
my temp file is :
1. A100:Annie Marie's Dance Academy:123:1500
2. A100:Annie Marie's Dance Academy:254:500

then i am using the command:
awk -F ":" 'BEGIN{printf "Purchases Detail\n===============\n\n"}{printf "Vendors Name :%s\n",$2}{printf "Invoice# %i\t",$3}{printf "Amount:$%5.2f\n",$4}{print $1}' temp2

which is giving me this output:
Enter the vendor number: A100
Purchases Detail
=================

Vendors Name :Annie Marie's Dance Academy
Invoice# 123 Amount:$123.00
Vendors Name :Annie Marie's Dance Academy
Invoice# 254 Amount:$254.00

but i need this output:
Vendor Name: Annie Marie's Dance Academy

1. Invoice #123 Amount: 1500
2. Invoice #254 Amount: 500
Now the problem is how to store the value of second field in a variable by separating it from the entire file.

Thanks in advance
 
Old 03-27-2010, 08:53 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
^ i am not so l33t with awk so i did it in two separate commands:
Code:
[liveuser@localhost ~]$ awk -F ":" 'BEGIN{printf "Purchases Detail\n===============\n\n"}{printf "Vendors Name :%s\n",$2}' temp.lst | uniq; \
 awk -F ":" '{printf "Invoice# %i\t",$3}{printf "Amount:$%5.2f\n",$4}' temp.lst | grep -n . | sed  s/:/". "/
Purchases Detail
===============

Vendors Name :Annie Marie's Dance Academy
1. Invoice# 123	Amount:$1500.00
2. Invoice# 254	Amount:$500.00
 
Old 03-27-2010, 09:06 AM   #7
szahmad1
LQ Newbie
 
Registered: Nov 2009
Posts: 14

Original Poster
Rep: Reputation: 1
Thanks schneidz and grapefruitgirl for your help.I deeply appreciate your help.
 
  


Reply



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
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
Using Variables gregarion Programming 1 02-04-2010 02:26 PM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 12:30 PM
variables? Soulful93 Programming 7 06-13-2004 12:24 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:06 PM.

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
Open Source Consulting | Domain Registration