LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 04-10-2005, 08:21 PM   #1
Seventh_Warrior
LQ Newbie
 
Registered: Feb 2004
Location: Canada
Distribution: Fedora Core 3 x86_64
Posts: 19

Rep: Reputation: 0
Replacing text in a specific field


I'm trying to write a bash script that will find a user in the password file and replace the text in the comment field with something I type in. I've got the finding the user part, but I'm not sure how I would go about replacing the text in the field.

Any ideas?
 
Old 04-10-2005, 08:47 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Do you want to see what you're going to replace,
or just slap something over it in the first place?

And which tools do you want to use, what have
you done so far?


Cheers,
Tink
 
Old 04-10-2005, 09:02 PM   #3
Seventh_Warrior
LQ Newbie
 
Registered: Feb 2004
Location: Canada
Distribution: Fedora Core 3 x86_64
Posts: 19

Original Poster
Rep: Reputation: 0
So far I've used grep to find the line in the passwd file containing the username, and awk to display the fifth field.
I've asked the user for input, now I just have to write it to the file.
 
Old 04-10-2005, 09:20 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I'd suggest building the entire line in the shell-script,
remove the matching line in place using sed, and then
adding the new line at the end using an
echo $NEW_LINE >> /etc/passwd ...

It's probably possible to do it in place with sed but
the interaction of extracting the user-variable, sed and
eval may turn out to be quite ugly and hard to read :)


Cheers,
Tink
 
Old 04-10-2005, 09:22 PM   #5
Seventh_Warrior
LQ Newbie
 
Registered: Feb 2004
Location: Canada
Distribution: Fedora Core 3 x86_64
Posts: 19

Original Poster
Rep: Reputation: 0
I'll give it a shot, thanks.
 
Old 04-11-2005, 12:04 PM   #6
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
i think gawk would be the tool to use for this, it was created for manipulating fields. gawk could match the first field witht he user name, and then replace field 5 with your comment.

#! /bin/bash
gawk -F ":" -v OFS=":" '$1 != USR {print $0}; $1 == USR {print $1,$2,$3,$4,COMMENT,$6,$7};' USR=$1 COMMENT=$2 $3

say this script is called comment_replace

>comment_replace user_name new_comment /etc/passwd

would print out the new passwd file to stdout, that way you can check to make sure it looks right, then run the same command but redirect the output to a tmp file and then replace /etc/passwd

>comment_replace user_name new_comment /etc/passwd > tmp
>mv tmp /etc/passwd
 
  


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
replacing text in file kushalkoolwal Programming 17 10-25-2005 05:25 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
How to insert same text in one field for 10,000 records table edhan Linux - Newbie 6 06-13-2005 03:48 AM
javascript: change value of text field and submit codec Programming 3 06-06-2005 08:08 AM
replacing specific lines in a text document stellarmarine1 Linux - General 1 09-07-2004 02:34 PM

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

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