LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-07-2012, 11:17 AM   #1
tastiero
LQ Newbie
 
Registered: Mar 2012
Location: Rome (Italy)
Posts: 17

Rep: Reputation: Disabled
sed use - extract substring from lines of text file


hello everybody.
i'm looking for a code to use sed command to extract a substring from the lines of a text file, like the example:

.......
ew7yweryu
gh34234hj3.cfg 23l
34gh5435385h45f.cfg gf887
sd634g6347.cfg 775t
3ff83g84tg34tt4t9.cfg 534h
f7s87d6g78g.cfg 4774t
f4fh7384t34334tsdf.cfg 5747
dsfahjr8
.......

i would cut the end of line start from the space only for the lines that contains filename.cfg and cut the oethers like this:

gh34234hj3.cfg
34gh5435385h45f.cfg
sd634g6347.cfg
3ff83g84tg34tt4t9.cfg
f7s87d6g78g.cfg
f4fh7384t34334tsdf.cfg

can anyone help me about the code?
thanks a lot.

Last edited by tastiero; 03-07-2012 at 12:11 PM.
 
Old 03-07-2012, 12:17 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
What have you tried? Here is a link if you do not know where to start: http://www.grymoire.com/Unix/Sed.html
 
1 members found this post helpful.
Old 03-07-2012, 12:31 PM   #3
tastiero
LQ Newbie
 
Registered: Mar 2012
Location: Rome (Italy)
Posts: 17

Original Poster
Rep: Reputation: Disabled
i've tried something like this but it's little test, i miss the part to cut the end of line after the space after .cfg
sed -rn '\|cfg|p' output.txt
 
Old 03-07-2012, 01:32 PM   #4
tastiero
LQ Newbie
 
Registered: Mar 2012
Location: Rome (Italy)
Posts: 17

Original Poster
Rep: Reputation: Disabled
i've tried also this:

sed -nr '/^[a-z0-9]+[.]+[a-z]+$/ p' output.txt

but i can only see the lines that contains only filename.cfg. the others lines (e.g. filename.cfg 54434) doesn't appear on output.

please help me. i'm new.
 
Old 03-07-2012, 01:44 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi,

this worked for your sample file:
Code:
$ cat file
ew7yweryu
gh34234hj3.cfg 23l
34gh5435385h45f.cfg gf887
sd634g6347.cfg 775t
3ff83g84tg34tt4t9.cfg 534h
f7s87d6g78g.cfg 4774t
f4fh7384t34334tsdf.cfg 5747
dsfahjr8

$ sed '/\.cfg/!d;s/ .*//' file
gh34234hj3.cfg
34gh5435385h45f.cfg
sd634g6347.cfg
3ff83g84tg34tt4t9.cfg
f7s87d6g78g.cfg
f4fh7384t34334tsdf.cfg
Let us know if that is what you are looking for.

PS: Welcome to LQ.

Last edited by crts; 03-07-2012 at 01:45 PM.
 
1 members found this post helpful.
Old 03-07-2012, 04:06 PM   #6
tastiero
LQ Newbie
 
Registered: Mar 2012
Location: Rome (Italy)
Posts: 17

Original Poster
Rep: Reputation: Disabled
it works very very fine, it's just that i want. thank you very much.
 
Old 03-07-2012, 11:05 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
And another alternative:
Code:
sed -n '/cfg/s/ .*//p' file
 
1 members found this post helpful.
Old 03-08-2012, 12:15 AM   #8
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
Similar solution with explanation for people who need it

Quote:
sed -e '/.cfg/!d; s/ .*/ /'
/.cfg/ - will match any line with the .cfg and !d says do not delete such a line when it is found.


s/ .* - says substitute .(a character) * (repeat a previous character) and substitute it with a blank line // or / /
(s/ .*/ /)
 
1 members found this post helpful.
Old 03-08-2012, 01:20 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
@cbtshare - I think you need to look at your definitions again as they are not correct (completely) ... eg. /.cfg/ - will not just match .cfg but also Acfg as the dot (.) here means the same as in your
second explanation.
 
1 members found this post helpful.
Old 03-08-2012, 02:29 AM   #10
tastiero
LQ Newbie
 
Registered: Mar 2012
Location: Rome (Italy)
Posts: 17

Original Poster
Rep: Reputation: Disabled
thanks to everybody for the help.
 
Old 03-08-2012, 03:07 AM   #11
tastiero
LQ Newbie
 
Registered: Mar 2012
Location: Rome (Italy)
Posts: 17

Original Poster
Rep: Reputation: Disabled
another little help.
now i have the right file:

asdfa23e23def.cfg
df73rdfdf.cfg
9h9fdhisfgd.cfg
......
......

how can i insert at the start of each line a string of web address:

http://mysite/configuration/asdfa23e23def.cfg
http://mysite/configuration/df73rdfdf.cfg
http://mysite/configuration/9h9fdhisfgd.cfg
......
......

thanks again
 
Old 03-08-2012, 10:09 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Did you look on the link I provided?
 
Old 03-08-2012, 11:29 PM   #13
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
@grail , thanks for the correction.
 
Old 03-09-2012, 02:59 AM   #14
tastiero
LQ Newbie
 
Registered: Mar 2012
Location: Rome (Italy)
Posts: 17

Original Poster
Rep: Reputation: Disabled
i've tried
sed 's/^/http:\/\/mysite/' output.txt

and no problem but when i use metacharacters of web link e.g. / . or web link that starts with numbers i have problems about syntax or about output results.
i've tried to put the web link in a string ant then pass it to the command e.g. $address, but same problems.

i try again a try to learn, i like to learn.
somebody can help me?
thanks.
 
Old 03-09-2012, 03:43 AM   #15
tastiero
LQ Newbie
 
Registered: Mar 2012
Location: Rome (Italy)
Posts: 17

Original Poster
Rep: Reputation: Disabled
i've found this good pages.
http://www.ibm.com/developerworks/li...ed1/index.html
http://www.ibm.com/developerworks/li...ed2/index.html

i'm learnig
 
  


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
[SOLVED] Extract a substring using regular expression with SED PenguinJr Programming 9 05-11-2011 01:47 PM
Extract substring using sed hweontey Linux - Newbie 1 02-22-2011 04:27 AM
[SOLVED] Extract multiple lines of data from a text file. shawnamiller Programming 8 04-30-2010 12:46 PM
extract substring using sed and regular expressions (regexp) lindylex Programming 20 12-22-2009 11:41 AM
Extract lines NOT on a block of text from a file Renan_S2 Programming 3 10-05-2008 05:14 PM


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