LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 01-13-2012, 05:05 AM   #1
daleo
LQ Newbie
 
Registered: Nov 2005
Posts: 7

Rep: Reputation: 0
Modifying text file with "one command line" SED command...


Hello!

I have a text file like:

start_label
this is test 1234 wow
this is test 3456 wow
this is test 5463 wow
end_label


I need to bring it to such format:

1234,
3456,
5463


As you see, first and last strings should be deleted, each string should be truncated to one substring value (numeric), each result string should end with comma (",") except the last one.

Question: Is it possible to do without CUT/AWK constructions? Just with SED ?

Thanks!!
 
Old 01-13-2012, 05:13 AM   #2
j_h
LQ Newbie
 
Registered: Oct 2011
Posts: 26

Rep: Reputation: Disabled
the following will remove everything except the digits:

cat oldfile | sed 's/[^0-9]//g' <oldfile >newfile

Adding commas before all the newlines except one is more involved, I'll come back to the thread later if I get a few minutes to work out exactly how to get the result you want.

Last edited by j_h; 01-13-2012 at 05:20 AM.
 
Old 01-13-2012, 05:31 AM   #3
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 189Reputation: 189
This may be rough (rough is a speciality of mine), and it uses grep to exclude the start and end label tags. But it works..

Code:
~/tmp $ cat test
start_label
this is test 1234 wow
this is test 3456 wow
this is test 5463 wow
end_label
~/tmp $ grep -v 'label' test | sed -e 's/[^0-9]//g' -e 's/$/,/' -e '$s/.$//'
1234,
3456,
5463
's/[^0-9]//g' - removes anything that isnt numerical
's/$/,/' - appends "," to the end of the line
'$s/.$// - removes last character from the stream (the final ",")

Last edited by fukawi1; 01-13-2012 at 05:33 AM.
 
Old 01-13-2012, 05:32 AM   #4
Roken
Member
 
Registered: Oct 2011
Location: Bolton, UK
Distribution: Arch local, Debian on VPS
Posts: 252

Rep: Reputation: 40
This will do what you want except remove the last comma - not sure it can be done in a single sed command:
Code:
sed -e '1,1d' -e '$d' -e 's/.* \([0-9]*\) .*/\1,/g'
EDIT: OK, got it - one liner. To preserve the original file and write to a new file:
Code:
sed -e '1,1d' -e '$d' -e 's/.* \([0-9]*\) .*/\1,/gw  out.txt' test.txt;  sed -i '$s/.$//' out.txt
or to overwrite the original:

Code:
sed -i -e '1,1d' -e '$d' -e 's/.* \([0-9]*\) .*/\1,/g' test.txt;  sed -i '$s/.$//' test.txt

Last edited by Roken; 01-13-2012 at 05:49 AM.
 
  


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
sed command to replace line in text file gengwei89 Linux - Newbie 6 11-06-2011 08:10 AM
Trying to use cut command to limit "excess text" from a file. Hank The Tank Linux - Newbie 2 02-24-2011 06:12 PM
[SOLVED] trying to insert text in the last line of a file with sed command.... Sayan Acharjee Linux - General 7 10-04-2010 05:00 AM
bad: Want to insert a line into a text file using "sed" command eliote Linux - General 7 09-19-2010 02:55 AM
sed error "command c expects \ followed by text" under OS X (but works in Linux) srunni Programming 3 12-02-2009 02:52 AM


All times are GMT -5. The time now is 09:36 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration