LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-30-2009, 12:09 PM   #1
ashish0603
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
Help in AWK


hello all..
i am stuck in awk...i need to modify a field inside a file using awk..

here is my code...

--------------------
if test -e STD.dat
then
echo "Enter Roll Number whose record is to be modified:"
read rno;
#now check if any record for that roll no. exists
awk 'BEGIN{
flag=0
}
{
if($1 == '$rno')
{
print $0>>"data1"
printf("Enter Name:")
getline name<"/dev/tty";
printf("Enter average marks:");
getline avg<"/dev/tty";
#here am i making a mistake???
$2 = name;
$3 = avg;
print $0
#here it prints the new values on the screen...but the same isn't #reflected in the database(file,i mean).
flag=1;
}

}END{
if(flag==1)
{
print "modified something..."
}
else
{
printf("\nRecord not found in database.");
}
}' STD.dat
fi
--------------------

ok...so i just pasted my code here..i dont know the rules of this forum..i'll check them out soon... i hope i haven't violated many rules in my 1st post itself..
pls help me guys....badly stuck at this problem...
 
Old 09-30-2009, 12:36 PM   #2
AFI
LQ Newbie
 
Registered: Nov 2005
Distribution: Fedora Core 4
Posts: 7

Rep: Reputation: 0
You forgot to mention what the problem is.
By the way, you're using "'" to delimit the awk script, so you need to replace the apostrophes from within the awk code. Like in '$rno', make it "$rno" and also on your comment "isn't" to "is not"
 
Old 09-30-2009, 12:46 PM   #3
ashish0603
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by AFI View Post
You forgot to mention what the problem is.
By the way, you're using "'" to delimit the awk script, so you need to replace the apostrophes from within the awk code. Like in '$rno', make it "$rno" and also on your comment "isn't" to "is not"
Thanks,but the thing is the correct record is getting detected from the database.so "rno" shouldn't be an issue..
my problem is that the record isnt getting updated inside the file..i mean its not written in the file,although $2,$3 are updated..i have written that in comment part...
 
Old 09-30-2009, 12:52 PM   #4
AFI
LQ Newbie
 
Registered: Nov 2005
Distribution: Fedora Core 4
Posts: 7

Rep: Reputation: 0
You were expecting those values to be updated on file "data1" ?
That file is being written before you acquire the values, not after.
 
Old 09-30-2009, 12:59 PM   #5
ashish0603
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by AFI View Post
You were expecting those values to be updated on file "data1" ?
That file is being written before you acquire the values, not after.
hey,i wrote in "data1" file.But if i want to modify in the same file(STD.dat) how do i do that? In C/C++,we normally position our write pointer to that place and then use write() function..so here,in awk, how do i do that?
 
Old 09-30-2009, 01:07 PM   #6
AFI
LQ Newbie
 
Registered: Nov 2005
Distribution: Fedora Core 4
Posts: 7

Rep: Reputation: 0
Sorry, I'm not aware of any way to write back into that file.
 
  


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
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 07:23 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 01:21 PM
using awk on a awk result jpgauvin Programming 1 12-15-2008 04:57 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 06:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:57 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