Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
As a linux/unix noob trying to teach myself how to use the command line, I am confused about using sed.
Can someone give me an example of some sed commands that
1) removes leading blanks spaces in a sentence
2) removes trailing blank spaces in a sentence
3) removes unnecessary blanks between words( so intstead of HI____my name is, it should be HI_my name is. _ representing blank spaces)
4) deletes blank lines
I will use these commands to print the old file to a new file which I do know how to do.
You should try to accomplish this, or whatever attempts you have tried, please post what you've done. Which is to say that it's more appropriate to give it a try and show at least that you've attempted it and are not just listing a bunch of items you want someone else to do for you.
Suggestions as to how to proceed:
- Look at the manpage for sed, it will tell you the semantics of the command.
- Google for sed examples, and you can google for very specific examples; likely some of which will match exactly what you want.
- Know that what sed does for you is to take in either from a file or from a pipe and then outputs to stdout.
That last item is a good tip to consider, and I think very helpful to avoid personal "oops" occurrences.
Taking in from a file is, to me; normal. But you can also channel the output of another command into sed; for instance a grep of a pattern from a file. Say the file contains many lines of varying content, but you can find some smaller group of those lines which you care about by using grep. You can pipe the output of that grep into sed. Next sed will do the edits you want; such as a search and replace of patterns, and search/replace can search for pattern and replace with nothing, thus erasing the pattern. And it sends that output to stdout, or your console. You can then choose to redirect that output to some other file. The result is that you make some sed command which you're not sure of, and believe me I do it that way, because I always have to google and figure out what I need to do to accomplish my sed goal, I don't use it everyday. And the beauty I see is that it won't overwrite your input file, causing that form of personal "oops" which I mentioned before.
Learn some regex. For your task you need to know that '^' matches the beginning of the line, '$' matches the end of the line, and you can match a minimum and maximum number of the previous character with '{min,max}'.
An example for removing more than one blank spaces between words:
Code:
echo 'test test test' | sed -r 's/ {2,}/ /g'
You need the '-r' for extended regex which allows use of '{min,max}' and others.
You should try to accomplish this, or whatever attempts you have tried, please post what you've done. Which is to say that it's more appropriate to give it a try and show at least that you've attempted it and are not just listing a bunch of items you want someone else to do for you.
Suggestions as to how to proceed:
- Look at the manpage for sed, it will tell you the semantics of the command.
- Google for sed examples, and you can google for very specific examples; likely some of which will match exactly what you want.
- Know that what sed does for you is to take in either from a file or from a pipe and then outputs to stdout.
That last item is a good tip to consider, and I think very helpful to avoid personal "oops" occurrences.
Taking in from a file is, to me; normal. But you can also channel the output of another command into sed; for instance a grep of a pattern from a file. Say the file contains many lines of varying content, but you can find some smaller group of those lines which you care about by using grep. You can pipe the output of that grep into sed. Next sed will do the edits you want; such as a search and replace of patterns, and search/replace can search for pattern and replace with nothing, thus erasing the pattern. And it sends that output to stdout, or your console. You can then choose to redirect that output to some other file. The result is that you make some sed command which you're not sure of, and believe me I do it that way, because I always have to google and figure out what I need to do to accomplish my sed goal, I don't use it everyday. And the beauty I see is that it won't overwrite your input file, causing that form of personal "oops" which I mentioned before.
How would you put all of these in a file and run them together? This is where I cannot seem to find much. I want to place those commands in a file run the file and save the results to another file.
Last edited by michaelgg13; 04-16-2014 at 01:05 PM.
My guidance points (and check that last link it's my blog entry on the topic), are to stay fundamental, understand that a "script" is essentially the same as commands you can run from the command prompt. Therefore if you can "type" it, you can "script" it.
To do these sed commands the way you want, you should write them as part of a script:
Code:
#!/bin/bash
# comment line - this is my script to do what I want
sed-command-1
sed-command-2
How would you put all of these in a file and run them together? This is where I cannot seem to find much. I want to place those commands in a file run the file and save the results to another file.
3 is NOT correct, unless it does what you want it to do. Here's what I get:
Code:
bash-4.2$ echo 'test test test' | sed 's/ */\ /g'
t e s t t e s t t e s t
This one isn't as easy as the others, so that's why I posted the answer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.