LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Modifying text file with "one command line" SED command... (http://www.linuxquestions.org/questions/linux-newbie-8/modifying-text-file-with-one-command-line-sed-command-923575/)

daleo 01-13-2012 06:05 AM

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

j_h 01-13-2012 06:13 AM

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.

fukawi1 01-13-2012 06:31 AM

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 ",")

Roken 01-13-2012 06:32 AM

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


All times are GMT -5. The time now is 07:27 PM.