LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 12-29-2011, 03:23 PM   #1
chris_carr
Member
 
Registered: Oct 2011
Location: Houston Tx
Distribution: RHEL 6
Posts: 57

Rep: Reputation: 0
Using SED to edit a file


Hello again,


I'm currently writing a program that will automate most of the work when adding a new user to our NIS server.

I have 80% of the script done and all I need now is a way to use SED to edit some files.

ex:

#add user to passwd file

echo -n " Add user information and press [ENTER]: "
read value
sed ???????
###################################################

so I basically want the script to take the input from the user at the keyboard and put what is typed into the particular file.


Im sure this will be an easy one for most of you.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-29-2011, 03:37 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
1) Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.

2) How do you know that sed is the correct tool to use here? It may indeed be, but it's a good idea not to limit your options when asking how to do something.

3) How about some more details? What is the format of the file, and exactly where does the new text need to be inserted? How is the input point determined, by line number, text pattern, just tacked onto the end, or what?

4) What have you tried to do so far, and what are the results?


Here are a few useful sed references for you to start with:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt


You may want to pay particular attention to the i and a commands.
 
2 members found this post helpful.
Old 12-29-2011, 03:50 PM   #3
chris_carr
Member
 
Registered: Oct 2011
Location: Houston Tx
Distribution: RHEL 6
Posts: 57

Original Poster
Rep: Reputation: 0
1. its pretty obvious to anyone who knows linux what part is the code...and if its not ex: = example

2. If you have a better suggestion for a tool to use to edit a script without opening a txt editor then please share.

3. Its an NIS passwd file. If you have never seen one then I do not think I could explain that to you. it looks alot like an /etc/passwd file on a local system.

4. I have not tried anything else beacause after 3 days of googleing everything says to use sed.

I apprecaite the refferences but i'm asking for help with the syntax not for more articles to read lol.
 
Old 12-29-2011, 04:06 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
The 3) matters as if the job is to insert a line at the end of your file, simple echo command is ok

Like:
Code:
echo "your formated line" >> file
 
1 members found this post helpful.
Old 12-29-2011, 04:43 PM   #5
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 6,305

Rep: Reputation: 1720Reputation: 1720Reputation: 1720Reputation: 1720Reputation: 1720Reputation: 1720Reputation: 1720Reputation: 1720Reputation: 1720Reputation: 1720Reputation: 1720
I'd write it in VBScript and use Writeline.
 
1 members found this post helpful.
Old 12-30-2011, 07:50 AM   #6
chris_carr
Member
 
Registered: Oct 2011
Location: Houston Tx
Distribution: RHEL 6
Posts: 57

Original Poster
Rep: Reputation: 0
@273: VB??? I'm pretty sure that will just over complicate everything.

@Cedrik: I'm not really sure what you are saying here...and I'm not really sure you understood my question.


Pretty simple piece of code here.

The first part of my script all I do is create the username, change the ownership, the file permissions. After that is done the script logs into our NIS server. From here there are 3 files that need to be edited. 1. passwd file, 2. domain.map 3. shadow.


So again the 2nd half of my script will look like this:
#######################################
code:
ssh nisserver
cd /var/yp/source
echo -n " please add userline to passwd file and press [ENTER]: "
read $passwd
sed "THIS IS WHERE IM LOOKING FOR HELP WITH THE SYNTAX"
#######################################

So basicly I want what ever the user types in to be added to the passwd file. Now like I said before I did a lot of research on this before I even posted on here and everything says sed is the best tool...and since I have been doing this awhile I would have to agree. Sed is extremely powerful.


So if anybody understands my question then please help me out.
 
Old 12-30-2011, 07:56 AM   #7
Roken
Member
 
Registered: Oct 2011
Location: Bolton, UK
Distribution: Arch local, Debian on VPS
Posts: 258

Rep: Reputation: 41
The reason for using /code tags is not because we don't know when reading your post which parts are code, but it makes things a lot easier to read. The tags are included for a reason.

As for how you do it, your question is simply not specific enough. We need to know exactly what information you are expecting the user to input and in what format (" please add userline to passwd file and press [ENTER]: " doesn't tell us enough) and we need to know the exact format of the line where the information is expected to be inserted (and the insertion point on that line). sed is indeed very powerful, but it isn't psychic and needs to be told precisely what it's supposed to do.
 
2 members found this post helpful.
Old 12-30-2011, 08:14 AM   #8
chris_carr
Member
 
Registered: Oct 2011
Location: Houston Tx
Distribution: RHEL 6
Posts: 57

Original Poster
Rep: Reputation: 0
word.


What the user will input will look like this

[code]
username:x:96:96::/home/username:/bin/sh


what I'm wanting sed to do is take that line and add it to the /var/yp/source/passwd file. hope I used the code feature correctly.
 
Old 12-30-2011, 08:23 AM   #9
Roken
Member
 
Registered: Oct 2011
Location: Bolton, UK
Distribution: Arch local, Debian on VPS
Posts: 258

Rep: Reputation: 41
The code tags are used with [code]Code block[/code]

As for using sed, looks like you are simply appending the line to the end of the file, in which case the echo command posted by Cedrik is a better option.

Code:
echo ${passwd} >> /var/yp/source/passwd
 
1 members found this post helpful.
Old 12-30-2011, 08:32 AM   #10
chris_carr
Member
 
Registered: Oct 2011
Location: Houston Tx
Distribution: RHEL 6
Posts: 57

Original Poster
Rep: Reputation: 0
o ok. I will give it a shot and let you know. Thanks guys.
 
Old 12-30-2011, 08:50 AM   #11
chris_carr
Member
 
Registered: Oct 2011
Location: Houston Tx
Distribution: RHEL 6
Posts: 57

Original Poster
Rep: Reputation: 0
nope that did not work.



Code:
 

cd /home/username

echo -n " type some stuff in and press [enter]: "
read $stuff
echo ${stuff} >> /home/username/stuff
 
Old 12-30-2011, 08:53 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
As already mentioned the straightforward method to append a new line to a file is by means of echo with >> double redirection. To accomplish the same in sed, you need the a command and match the end of file with $
Code:
sed '$a new line' file
However, since you have the user's input stored in a shell variable, you cannot use single quotes (that prevent shell expansion). Using double quote you have to protect the $ pattern from the shell, anyway. Here is an example:
Code:
sed "\$a $passwd" /var/yp/source/passwd
By default sed sends the result to the standard output. In order to edit the file in place, that is to actually change its content, you need the -i option.
Code:
sed -i "\$a $passwd" /var/yp/source/passwd
You can also save a backup copy of the original file, using a custom suffix as argument of the -i option, e.g.
Code:
sed -i.bck "\$a $passwd" /var/yp/source/passwd
Just a final note on your script. The line
Code:
read $passwd
is incorrect. It should be
Code:
read passwd
without the $ sign. This means you have to specify the variable name in the read statement, not its value! Hope this helps.
 
Old 12-30-2011, 08:54 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by chris_carr View Post
nope that did not work.

read $stuff
echo ${stuff} >> /home/username/stuff
This is for the reason mentioned at the end of my previous post.
 
1 members found this post helpful.
Old 12-30-2011, 10:32 AM   #14
chris_carr
Member
 
Registered: Oct 2011
Location: Houston Tx
Distribution: RHEL 6
Posts: 57

Original Poster
Rep: Reputation: 0
@colucix....you are the man sir. Thank you. it works now.
 
Old 12-30-2011, 01:50 PM   #15
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Be sure to quote your variable first, otherwise the value gets word-split on expansion. You want it to be echoed as a single block.

Code:
echo "${stuff}" >> /home/username/stuff
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
1 members found this post helpful.
  


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 do you use sed to edit a file? SparceMatrix Linux - General 3 09-04-2010 04:13 PM
using sed to edit html tkeyser Programming 6 08-20-2009 08:15 PM
SED: edit ip address n1wil Linux - Software 4 01-08-2008 03:18 PM
help to edit Edit my bootloader's config file from kernel prompt jagdishpandey Linux - Kernel 5 07-03-2007 07:59 AM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 09:24 PM


All times are GMT -5. The time now is 04:49 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration