LinuxQuestions.org
Visit Jeremy's Blog.
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 07-10-2012, 03:41 AM   #1
shiftshifter
LQ Newbie
 
Registered: Jul 2012
Posts: 7

Rep: Reputation: Disabled
Editing And Deleting A Line From A Text File Using Shell Script


Hi,

I'm new in using shell script. I'm trying to write a script where you can add, edit or delete information saved in a text file. I have already managed to adding part but I'm having difficulty with the other two. I tried online samples as basis for my scripts but I can't seem to make them work. Especially sed and awk. From my readings, I think they are the ones I need to use. So here is my problem:

Suppose I have a file named "changeletter.sh" and a text file named "letters.txt" which contains the following:
a d f s w a s x d f c x s
r f t a s w a d f r e d s

After running the script, I would then input the letter I want to change and what the new letter will be. Say change the letter a's to z. Or to delete all the letter f's.

Any help would be greatly appreciated.
 
Old 07-10-2012, 08:18 AM   #2
xerox
Member
 
Registered: Mar 2011
Posts: 43

Rep: Reputation: 0
Wink

Hi ShiftShifter!

I have given a script that does the basic requirements you asked for. Please include all the other error handling you require.

Code:
#!/bin/sh 
echo "Enter letter to be changed and letter to be changed with"
read a b
cat letter.txt | sed 's/'$a'/'$b'/g' > temp.txt
echo "Enter the letter to be deleted"
read c
cat temp.txt | sed 's/'$c'//g' > letter.txt
cat letter.txt
Please let me know incase of any concern.

Xerox
 
Old 07-10-2012, 09:19 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
There's no simple answer to this. Each operation really needs to be addressed separately, and some tools are better suited to certain operations than others. Changing all of one character to another, for example, or deleting them, is really best done with tr, although sed is capable of it.

Xerox's example above covers the basic substitution feature of sed, but there are a couple of weaknesses with it I need to address.

First, Useless Use Of Cat. sed can (and should) read files directly.

Second, the quoting. He's attempted to let the shell variables expand by unquoting them in the expression. This isn't safe. The usual way to handle it is to simple double-quote the whole string.

To explain in more detail, you should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


Third, you can apply multiple expressions at once using the -e option, and you can edit a file in place with the -i option (and optionally save the original to a backup file), so you don't need a temp file.

Code:
sed -i.bckp -e "s/$a/$b/g" -e "s/$c//g" letter.txt
Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/


For other tools like tr, start by calling up "info coreutils". There are also info pages for grep and sed.

Regular expressions are a core function of many of these tools and others, and learning them is one of the best bang-for-the-buck uses you can put your study time to.

Here are a few regular expressions tutorials:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html
http://www.regular-expressions.info/


Finally, you might consider using ed for some of this. As a real, and very light, universally available, text editor, it's a good choice for scripting out simple edits, such as deleting lines or moving them around.

How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)

Last edited by David the H.; 07-10-2012 at 09:27 AM. Reason: added links & some reorganizing
 
2 members found this post helpful.
Old 07-11-2012, 12:16 AM   #4
xerox
Member
 
Registered: Mar 2011
Posts: 43

Rep: Reputation: 0
Wink

Hi David,

Thanks for your strong review comments.
Im pretty new to shell scripting, i will consider all the points you have given me.

Xerox.
 
Old 07-11-2012, 10:55 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Not to worry. You're doing fine.
I'm just here to help people improve their scripting (and continue to improve my own).

Keep up the good work!
 
1 members found this post helpful.
Old 07-13-2012, 12:16 AM   #6
shiftshifter
LQ Newbie
 
Registered: Jul 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks xerox and David the H. Your inputs were really helpful. I've been busy these couple of days so I wasn't able to check this forum until now. I do have another question. Say for example I have a file named names.txt with the following contents.

#names.txt
Name 1: John Doe
Name 2: Jane Doe

I've managed to create a script using your suggestions to change either the first name or the last name. However, my problem now is how to change both (e.g. change name 2 to Mary Wayne). Can't I assign all strings after "Name 2:" into a variable to change it in one go?

Thanks again for your help!

Last edited by shiftshifter; 07-13-2012 at 12:17 AM.
 
Old 07-13-2012, 01:18 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'm not quite sure I understand what you're asking at the end there, but again, sed or ed, and an appropriate regex can handle it.

Code:
sed -i '/^Name 2/ s/:.*/: Foo Bar/' file.txt
printf '%s\n' '/^Name 2/ s/:.*/: Foo Bar/' 'w' | ed -s file.txt
Another option is to not worry about matching substrings and simply replace the entire line.

Code:
sed '/^Name 2/c Name 2: Foo Bar' file.txt
printf '%s\n' '/^Name 2/c' 'Name 2: Foo Bar' '.' 'w' | ed -s file.txt
I learned how to do all this from the links I gave you above. Check them out!
 
  


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
need to write a shell script for appending text into each line of a file nandakv4 Programming 9 08-15-2009 03:32 AM
shell script loop though text file:-1 line at a time. knockout_artist Linux - Newbie 2 05-04-2008 06:58 PM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 02:10 PM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 01:39 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM

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

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