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 06-29-2011, 02:23 PM   #1
implosions
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Rep: Reputation: 0
Angry Stream editor hell!


Phew, it took me longer to try to write a sed command, than to do what I wanted by hand.

Let me save you the introduction and go io the real deal.
I want do do this:

All files in the directory, remove the unnecessary extensions and output the results to a file? No problem!

test2.cfg
test1.cfg


$ ls | sed 's/.cfg//g' > output
(Note, there are about 300~ files for this command, I just added two now)

Eh, now we are getting to the real deal.
I want to do this:

function "(copy the last word of the line here)" excec test2.cfg

so, the result would be fairly simple

function "test2.cfg" exec test2.cfg
function "test1.cfg" exec test1.cfg

Now, since I am a newbie, I had no idea how to accomplish it.
So, I thought to do this:

$ sed 's/^/function " " exec /g' output > output2

And then!

$ sed "s/" "/$(cut output)/g" output2 > output3

But it won't work because the file goes multiple lines, not test2.cfg test1.cfg.
If the file "output" had only one line "test2.cfg", the command would work, but I would get:

function "test2.cfg" exec test2.cfg
function "test2.cfg" exec test1.cfg
function "test2.cfg" exec test3.cfg
function "test2.cfg" exec test6.cfg
function "test2.cfg" exec test2.cfg
function "test2.cfg" exec test5.cfg
function "test2.cfg" exec test2.cfg

Just a quick note, file names do not contain numbers

They are more something like this

hikns.cfg
behia.cfg
iaeiipuh.cfg

etc!

Anyone care to help me? Thank you VERY much, I have been playing with this all day.
 
Old 06-29-2011, 02:44 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
What about this?
Code:
sed 's/\(.*\)/function "\1" exec \1/' output
Take a look at http://www.grymoire.com/Unix/Sed.html#uh-4 for details.
 
1 members found this post helpful.
Old 06-29-2011, 02:58 PM   #3
implosions
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
What about this?
Code:
sed 's/\(.*\)/function "\1" exec \1/' output
Take a look at http://www.grymoire.com/Unix/Sed.html#uh-4 for details.
I love you! Thank you very much, it works perfectly combined with other commands

Code:
ls | sed 's/.map//g' | sed 's/\(.*\)/function "\1" exec \1/' > output
 
Old 06-29-2011, 09:51 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Two seds not required you just need to combine them:
Code:
ls -1 | sed -r 's/(.*)\.map/function "\1" exec \1/' > output
-1 is to list each file on its own line (note it is a one not the letter 'l')
-r is there to stop the need for escaping the brackets.

Last edited by grail; 06-29-2011 at 09:53 PM.
 
1 members found this post helpful.
Old 06-30-2011, 05:37 AM   #5
implosions
LQ Newbie
 
Registered: Jun 2011
Posts: 13

Original Poster
Rep: Reputation: 0
Beautiful, thank you
 
  


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
How to record a stream and start a new outputXXX.avi/mp3 for each new stream title ? frenchn00b Linux - General 4 08-04-2008 05:40 AM
knowing the stream editor SED yawe_frek Programming 2 12-31-2007 11:18 AM
sed (stream editor) problem igor.R Linux - Newbie 11 12-01-2007 10:44 PM
Howto transcode & relay a MPEG stream to a WMV stream?? crazyivan Linux - Software 0 06-15-2007 03:18 AM
Vi Editor From Hell Daniel Programming 7 07-19-2001 01:42 AM


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