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 06-29-2012, 02:08 PM   #31
joker20
Member
 
Registered: Sep 2004
Location: 127.0.0.1
Distribution: Slackware/Ubuntu/CentOS
Posts: 286

Rep: Reputation: 31

it will be a lengthy explanation but ill do my best!

the syntax is separated by pipes '|'...that should help you break it down a bit so with that in mind lets take a look

the first part says im going to search a text file for a string, in this case File: and replace it with nothing, in other words, delete it.
if the '\' between 'File' and ':' are confusing this is where a tutorial on sed/regular expressions will come in handy. the '\' is seen as an escape character to search for a metacharacter as a literal (literals are alphanumeric) because in regular expressions metacharacters have special meanings, in this case is the ':'
so if we were to just run the first part we would get the following:
Quote:
sed 's/File\: //g' *INFILENAME*
IMAGE1.txt
Resolution: 720 x 547
IMAGE2.txt
Resolution: 720 x 547
IMAGE3.txt
Resolution: 720 x 547
so we take this current output and run a second sed statement against it, this is where the pipe '|' comes in, its basically taking the output from the first statement and giving it to the second statement to be manipulated further.
so with that in mind we end up taking:
Quote:
Quote:
IMAGE1.txt
Resolution: 720 x 547
IMAGE2.txt
Resolution: 720 x 547
IMAGE3.txt
Resolution: 720 x 547
and will get...
Quote:
IMAGE1.txt
720 x 547
IMAGE2.txt
720 x 547
IMAGE3.txt
720 x 547
by using this syntax
Quote:
sed 's/Resolution\: //g'
notice how we dont need to specify the input file again...

so now that we have the content that we want we need to arrange it...this is where it could seem confusing at first but the 'man' pages for sed are very informative and actually describe the option i used.
Quote:
sed 'N;s/\n/ /g'
actually says to read a line 'N' then search for a new line '\n' and replace with a space. the space is necessary because without it the output would look like
Quote:
IMAGE1.txt720 x 547
- it will do this for all lines until its done...there are ways to specify how far down the text you want to read but thats not relevant at this time because we want to manipulate the entire content.
so the end result looks like:
Quote:
IMAGE1.txt 720 x 547
IMAGE2.txt 720 x 547
IMAGE3.txt 720 x 547
the last part
Quote:
> *OUTPUT*
is basically saying whatever the final result is to go ahead and write it to a file

to get a better idea of how the "N" works (read or append each line) add an additional N separated by a ';' - you would get the first 3 lines on 1


i hope this helps

Last edited by joker20; 06-29-2012 at 02:13 PM.
 
Old 06-29-2012, 04:43 PM   #32
brock_pace
LQ Newbie
 
Registered: Jun 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
how would one delete text using sed?
 
Old 06-29-2012, 04:45 PM   #33
joker20
Member
 
Registered: Sep 2004
Location: 127.0.0.1
Distribution: Slackware/Ubuntu/CentOS
Posts: 286

Rep: Reputation: 31
did you not even bother reading what i explained?
 
Old 06-29-2012, 05:17 PM   #34
usr345
Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 204
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by brock_pace View Post
how would one delete text using sed?
This command does it:

Code:
sed 's/Resolution: //g'
It takes a string and substitutes all the occurrences of 'Resolution: ' by and empty string.
------------------------------------------------------------------------------------------
For joker20:

'\' before ':' is not required, cause ':' is not a metacharacter in regular expressions.

------------------------------------------------------------------------------------------

For example, in the file input.txt I have the following text:

Code:
Resolution: 1 Resolution: 2 Resolution: 3
Resolution: 4
And run this command in shell:

Code:
sed 's/Resolution: //g' input.txt
I get the following output:

Code:
1 2 3
4
 
Old 06-29-2012, 05:23 PM   #35
joker20
Member
 
Registered: Sep 2004
Location: 127.0.0.1
Distribution: Slackware/Ubuntu/CentOS
Posts: 286

Rep: Reputation: 31
true - any non-alphanumeric character that i need to search for ill always throw a \ behind it just to be safe...for me its a best practice so you dont get unexpected results
thats just me though
 
Old 06-29-2012, 08:28 PM   #36
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Here's my SED one-liner:
Code:
sed -n '/IMAGE/ {N; s/File: //; s/\nResolution//; p}' filename > newfilename
brock;
Now that we have done the work for you, I hope you will look at the reference material we cited---study it until you understand what these commands are doing.
 
Old 06-29-2012, 08:57 PM   #37
joker20
Member
 
Registered: Sep 2004
Location: 127.0.0.1
Distribution: Slackware/Ubuntu/CentOS
Posts: 286

Rep: Reputation: 31
Quote:
Originally Posted by pixellany View Post
Here's my SED one-liner:
Code:
sed -n '/IMAGE/ {N; s/File: //; s/\nResolution//; p}' filename > newfilename
brock;
Now that we have done the work for you, I hope you will look at the reference material we cited---study it until you understand what these commands are doing.
interesting approach, i like it

heres one for just for kicks lol (im just trying to see how short i can get it)
Quote:
sed 's/F.*: //; s/R.*: //g' file | sed 'N;s/\n/: /g' > newfile
 
Old 07-02-2012, 11:04 AM   #38
brock_pace
LQ Newbie
 
Registered: Jun 2012
Posts: 23

Original Poster
Rep: Reputation: Disabled
im done everyobody, i used awk to only save the part after the colon and then echo to show both variables.
 
Old 07-02-2012, 05:01 PM   #39
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
brock you have learned a valuable lesson that there is always more than one way to solve a problem in Linux. However, I think what you really need here is some general knowledge about Linux and not specifically on sed. I used Linux for a few years before I typed my first sed command.


Bookmark these pages and get familiar with them:
http://tldp.org
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://ss64.com/bash

An Awesome linux/bash book: www.linuxcommand.org

Here's a couple cheet sheets you may want to reference(DON'T HANDICAP YOURSELF AND BECOME A COPY/PASTER! Learn what these do as you use them or need them):

http://www.thegeekstuff.com/2010/11/50-linux-commands/
http://g33kinfo.com/info/wp-content/.../04/comfu1.txt
 
1 members found this post helpful.
  


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
Yesterday's date in unix priyophan Linux - Server 10 03-31-2011 05:33 PM
yesterday had sound ,now don't! ggeo Linux - Hardware 15 11-22-2006 05:24 PM
Unix/Linux Programmer: How do I get started? peaceofcrap2001 Programming 7 06-14-2005 02:24 AM
Yesterday I had audio, today I don't. mimsmall Linspire/Freespire 1 08-09-2004 04:43 PM

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

All times are GMT -5. The time now is 04:47 AM.

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