Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-13-2012, 06:05 AM   #1
LQ Newbie
Registered: Nov 2005
Posts: 7

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


I have a text file like:

this is test 1234 wow
this is test 3456 wow
this is test 5463 wow

I need to bring it to such format:


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 ?

Old 01-13-2012, 06:13 AM   #2
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 06:20 AM.
Old 01-13-2012, 06:31 AM   #3
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 190Reputation: 190
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..

~/tmp $ cat test
this is test 1234 wow
this is test 3456 wow
this is test 5463 wow
~/tmp $ grep -v 'label' test | sed -e 's/[^0-9]//g' -e 's/$/,/' -e '$s/.$//'
'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 06:33 AM.
Old 01-13-2012, 06:32 AM   #4
Registered: Oct 2011
Location: Bolton, UK
Distribution: Arch local, Debian on VPS
Posts: 256

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:
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:
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:

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 06:49 AM.


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 09:10 AM
Trying to use cut command to limit "excess text" from a file. Hank The Tank Linux - Newbie 2 02-24-2011 07: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 06:00 AM
bad: Want to insert a line into a text file using "sed" command eliote Linux - General 7 09-19-2010 03:55 AM
sed error "command c expects \ followed by text" under OS X (but works in Linux) srunni Programming 3 12-02-2009 03:52 AM

All times are GMT -5. The time now is 11:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration