LinuxQuestions.org
Help answer threads with 0 replies.
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 05-28-2004, 12:54 PM   #1
kml100
LQ Newbie
 
Registered: May 2004
Location: Montreal
Distribution: Red Hat 7.0 / SuSe 9.0
Posts: 2

Rep: Reputation: 0
Linux sed command


Please Help, I am a student at Herzing college. We just started learning Red Hat Linux 7.0 (The version my teacher wants us to use.) I have been understanding everything up until this one command:

sed???

This is what we have to do:

#################################
# #
# #
# #
# #
# This is a test #
# #
#################################

We have to use sed to change the first column from # to *
Then change the last column from # to * using sed.

Please someone help. Thank You.
 
Old 05-28-2004, 01:18 PM   #2
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
I don't want to just post an answer; what have you tried so far?

Are you at all familiar with sed(1), or regexes in general? Please give more info!
 
Old 05-28-2004, 01:19 PM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Code:
#  somefile has the text to change   
# this does the first part, you do the second part 
# pipe the first sed command to your new one then 
# redirect to a file
cat somefile | sed 's/^#/*/' > newfile
#
 
Old 05-28-2004, 01:36 PM   #4
kml100
LQ Newbie
 
Registered: May 2004
Location: Montreal
Distribution: Red Hat 7.0 / SuSe 9.0
Posts: 2

Original Poster
Rep: Reputation: 0
I am not familiar with regexes, our teacher gave us a lab manual to work out of, we are learning most of the basic Linux text commands (cat, head, tr, ls, sed, etc.) It is a networking course, but we have to learn the basics first. The book we are using is: exam 101/102 LPIC 1 Certification Bible. I am on Chapter 4 P. 175. It gives a very vague description of the sed command. This is only our first week with Linux (Red Hat 7.0) We just finished learning text processing today (Friday May 28th, 2004) with the shell, we are not using the GUI at all. Thanks for your help, I hope this is enough info.
 
Old 05-28-2004, 03:27 PM   #5
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
NOTE: This post may be longer than it needed to be, or not long enough; I can't be sure. Hopefully it is clear enough. I think if you read it through, it should all make perfect sense . But I make no apologies .

The subject of regular expressions is large enough to fill a book, but I'll see if I can't give you the gist of it here.

Regular expressions are (sometimes complicated-looking) chunks of ascii characters. An example is the one which jim provided: 's/^#/*/'. The actual regular expression ("regex") in question here is the "^#" (the entire expression 's/^#/*/' is called a "substition"). Well, what are regexes for?

The reason regexes exist is to allow us to identify interesting portions of text within some larger block (or line) of text. There are a couple good reasons for wanting to "identify interesting portions of text"; we may want to pull interesting portions out, for use elsewhere or alter certain portions of text, in place. The text in question could be the manuscript for a book, an HTML page, or even the example you provided (8 or so lines of #'s, and some human english words).

Clearly your goal here is to alter certain portions, in place. You want to "change the first and last columns from # to *". Well, how do we do this? We need some way to identify the "first column" and the "last column" and then alter them somehow. Luckily, we have regexes. Regexes identify the portion of text, and programs like sed provide operations to alter, or otherwise use that text. The 's/regex/newtext' operation is one example, which sed provides (as do other programs: perl, awk, many more I can't think of atm).

Before we get to the regex, lets be clear on the syntax for the substition command:
Code:
s/regex/newtext/
The 's' is for "substitution", the regex is our regex, and the newtext is our new text.

So, I'm going to start with a simple example using grep (grep loves regexes):
Code:
$ echo "Not a speck of cereal" | grep "spalien acecraft"
$
Nothing happened! Why not? grep gobbled up our original text, compared it with our regex ("spalien acecraft") noted that it didn't match, and gave us the cold shoulder. Compare that with:
Code:
$ echo "Not a speck of cereal" | grep "t a sp"
Not a speck of cereal
$
Something happened! grep, being clever, noticed that "t a sp" exists in our original text: "Not a speck of cereal", so it spat the line back at us. What we've learned here is that, in a regex, normal characters match themselves (ie. an 'a' in our regex will match an 'a' in our original text, 'b' will match a 'b', etc.). That's simple enough.

Ok, play with that. It's pretty obvious (extremely useful though!). Now, what if we want to match something more complicated? We need magic. Magic is confusing. I can't teach you magic here! But I can explain the spell that Jim cast.

The '^' is magic; it matches "beginning of a line". So, by saying '^#', we're saying "identify any line which starts with '#'". Just to confuse things, here is another example of this:
Code:
grep "^#include" /usr/include/net/*.h
That will match all lines starting with "#include", in every header file in the "net" include directory. Massively interesting, I know...

So, getting back to our regex and our substition command to sed. 's/^#/*/'. Brilliant! Should be clear by now. What you need to do now, is find out how you can match the "end of a line". Now, depending on whether you want to match the end of each line, or just the first and last lines, you'll have to work some magic of your own. Think about the regex. What makes the first and last lines unique? How can you craft a spell to match only the endings of the first and last lines?

It takes a lot of trial and error. You may even think you've found an answer sometimes, but always carefully double-check it. You may be gobbling up more characters than you intended, or who knows what.

g/l

p.s. you can of course use multiple seds:
Code:
cat somefile |sed 's/regex/newtext/' |sed 's/regex/newtext/'
but a cleaner way (or not, depending on personal taste) is to use the -e option to sed. It works like this:
Code:
cat somefile |sed -e 's/regex/newtext/' -e 's/regex/newtext'
which allows multiple edits.

Last edited by rkef; 05-28-2004 at 09:10 PM.
 
  


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
sed command Kalyani1 Linux - Software 28 12-07-2005 11:48 AM
Linux Sed Command Help anirudh Programming 4 08-31-2004 02:09 PM
sed command linuxdev Linux - Newbie 9 02-24-2004 04:50 PM
sed Command linuxdev Linux - Newbie 3 02-09-2004 11:27 AM
sed command kwigibo Linux - General 3 04-21-2002 04:11 PM

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

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