LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 08-03-2012, 02:05 AM   #1
Divyavec
LQ Newbie
 
Registered: Aug 2012
Location: Chennai,India
Posts: 4

Rep: Reputation: Disabled
Question Replace a particular field in a particular line with user input using awk


I have a file called addr_book with the following data

FNAME LNAME ADDRESS PHONE COUNTRY
Harry Williams Virginia 1234 USA
Divya Purush Chennai 2098 India
Smith John Kentucky 1093 USA
Carol Stevens Wisconsin 3410 USa
Smith Williams sydney 5120 Aus
Feena Joseph veitname 1098 -

I have to edit the fname here.User enters the fname and lname of the person whose name i have to edit and also the new fname.With this I have to replace the fname.

I used the following

num1=$(awk -v new1="$lname" -v new="$fname" '($2==new1 && $1==new) {print NR}' addr_book)

echo "Enter the new fname:"
read new1
awk -v n="$choice" -v new="$new1" -v n1="$num1"
'{for (i=1;i<=5;i++) { if(($i=="$n") && (NR=="$n1")) $i="$new";print}' addr_book > addr_book.bak.

It is not giving the desired output...Can Some one help me pls?
 
Old 08-03-2012, 03:08 AM   #2
Chirel
Member
 
Registered: Nov 2009
Posts: 55

Rep: Reputation: 19
Hi,

Code:
fname=Smith
lname=John
newn=Garry
awk -v "lname=$lname" -v "fname=$fname" -v "newn=$newn" '$0 ~ fname" "lname {sub("^"fname,newn); print}' addr_book
 
1 members found this post helpful.
Old 08-03-2012, 04:08 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I agree with most of the last reply but see no reason to use sub:
Code:
awk -v "lname=$lname" -v "fname=$fname" -v "newn=$newn" '$0 ~ fname" "lname{$1 = newn}1' addr_book
 
1 members found this post helpful.
Old 08-03-2012, 04:19 AM   #4
Divyavec
LQ Newbie
 
Registered: Aug 2012
Location: Chennai,India
Posts: 4

Original Poster
Rep: Reputation: Disabled
Smile

Thanks a lot to both of you

What if i want to ask the user as which field to edit and edit tat field

echo "Enter the field you want to edit[1,2,3,4,5] :"
echo "1.First_name
2.last_name
3.Address
4.Phone
5.Country"
read choice
echo "Enter the new field:"
read new1

if the user enters 3,i have to edit Address field..Can i use for loop in this case?
 
Old 08-03-2012, 05:31 AM   #5
Divyavec
LQ Newbie
 
Registered: Aug 2012
Location: Chennai,India
Posts: 4

Original Poster
Rep: Reputation: Disabled
Question

This is the code i am using to achieve the above scenario...But its not working

awk -v "lname=$lname" -v "fname=$fname" -v "newn=$newn" -v "ch=$choice" '$1 ~ fname && $2 ~ lname{for (i=1;i<=5;i++) if (ch==$i) $i = newn}1 ' addr_book
 
Old 08-03-2012, 06:28 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Depends how much effort you want to put in I guess, here is one direction:
Code:
#!/usr/bin/awk -f

BEGIN{	
    getline list
    split(list, items)
    printf "Please enter first name: "
    getline fname < "-"
    printf "Please enter last name: "
    getline lname < "-"

    print "Select item from list below to be changed (enter corresponding number):"
    for( i = 1; i <= length(items); i++ )
	print i":",items[i]
    printf "> "
    getline num < "-"

    printf "Enter new data: "
    getline change < "-"

    print list
}

$1 == fname && $2 == lname{ $num = change }

1

Last edited by grail; 08-03-2012 at 06:30 AM.
 
Old 08-09-2012, 08:57 AM   #7
Divyavec
LQ Newbie
 
Registered: Aug 2012
Location: Chennai,India
Posts: 4

Original Poster
Rep: Reputation: Disabled
Question

@grail

This looks a bit complex.Isnt there a easy way to do this.

Cant we use a for loop like the below

awk -v "lname=$lname" -v "fname=$fname" -v "newn=$newn" -v "ch=$choice" '$1 ~ fname && $2 ~ lname{for (i=1;i<=5;i++) if (ch==$i) $i = newn}1 ' addr_book
 
Old 08-09-2012, 09:23 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
And where did you get all the variables from that you have created, ie lname, fname, newn, choice?

Once you have all the variables set, from mine you would only need:
Code:
$1 == fname && $2 == lname{ $num = change }
Remember I did say:
Quote:
Depends how much effort you want to put in
This a complete script and needs no extra input such as your shell variables (as mentioned above)
 
  


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
[SOLVED] Comparing two files and looking for the same line based on field - awk sopier Programming 8 12-26-2011 02:53 PM
awk command line: blank line record sep, new line field sep robertmarkbram Programming 4 02-21-2010 05:25 AM
Replace a field for a whole line in the same file, with awk. amwink Programming 12 11-13-2009 06:51 AM
awk to replace particular field vgr12386 Programming 9 06-05-2009 07:15 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM

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

All times are GMT -5. The time now is 08:38 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
Open Source Consulting | Domain Registration