sed: break before word if it's not last on the line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed: break before word if it's not last on the line
# 1  
Old 09-21-2010
sed: break before word if it's not last on the line

I've been trying this, and can't get it right. I want to put a line break before a word, but only if it's *not* the last word in the line. So if the break work was "fish," then...

We want to fish tomorrow

...would become...

We want to
fish tomorrow

...but this line would remain untouched:

We hope to catch lots of fish
# 2  
Old 09-21-2010
Code:
$ cat file1
We want to fish tomorrow
We hope to catch lots of fish
There's more fish in the sea
I love to eat fish fresh

$ sed 's/fish [^ ]*$/ \^J&/' file1
We want to  
fish tomorrow
We hope to catch lots of fish
There's more fish in the sea
I love to eat  
fish fresh

(^J comes from pressing Ctrl-v followed by Ctrl-j)

Try to show, next time, what you have tried, so that we could try to help your understanding.
# 3  
Old 09-21-2010
If my code was worth showing, I would. Smilie Seriously, I did last time I asked a question.

I'm not sure I'm doing this right. In vim on Ubuntu, I'm able to insert the Ctrl-J, which looks like ^@, but it doesn't do anything except make my output file illegible in gedit. When I look at it in vim, it hasn't made the line breaks.
# 4  
Old 09-21-2010
In that case, skip the Ctrl-v, Ctrl-j thing, just replace it by pressing Enter...

Code:
sed 's/fish [^ ]*$/ \
&/' file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed for deleting the first word of each line?

sed /'1-2'/&^/ filename suppose there is a file containing three lines , how do we do delete the word from each line? hyter efr frf rerfer efe ewd cdcf evrfgf erfv the output has to look like frf ewd erfv (2 Replies)
Discussion started by: Rajeev Nukala
2 Replies

2. UNIX for Dummies Questions & Answers

How to delete a particular word on particular line with sed?

I have a file from which I am trying to delete a particular word on a particular line. NEW NEW /v/engine NEW /ifs/list NEW /ifs/vdrome NEW I am trying to delete the first line only if it contains the word NEW. I am also trying to delete the last line only if it contains the word NEW. I... (11 Replies)
Discussion started by: newbie2010
11 Replies

3. Shell Programming and Scripting

Line break on word

I have a file that contains the following: ^field LINE_1 data ^field LINE_2 data ^field LINE_3 data ^field LINE_4 data ^field LINE_5 data ... And im looking to do a line break at the end of the number before the text to make it look like this ^field LINE_1 ... (11 Replies)
Discussion started by: darbs121
11 Replies

4. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

5. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

6. Shell Programming and Scripting

sed with line break

<td> CIS </td>and I tried to sed 's/<td>\/nCIS\/n<\/td>/<td><\/td>' and sed 's/<td>\/rCIS\/r<\/td>/<td><\/td>' , but no joy. This is an html page that I need to clean. (4 Replies)
Discussion started by: dba_frog
4 Replies

7. Shell Programming and Scripting

sed - deleting each line up to a word

Hi there, I'd like to delete the beginning of a line up until it finds a certain word or character string: in this case, I'd like to delete each line up to the word "mounting". Thanks ;) Susan (12 Replies)
Discussion started by: kitykity
12 Replies

8. Shell Programming and Scripting

move the last word to begining of next line - SED

Hello, I'm trying to move the last word of matching pattern to the begining of next line. Appreciate if anyone post the script. From the below line I'm getting the last word, Note: this word also appears in many places in my file #return the last word of line that contains ListenPort sed... (4 Replies)
Discussion started by: baskar
4 Replies

9. Shell Programming and Scripting

Sed Help (Using expression - line break)

*Note, I thought I was in Shell Programming and Scripting Q&A and posted in a wrong forum. To avoid confusion and to inform people that I'm not trying to "spam" the forums, I'm adding this note up top* Hi Everyone thanks in advance for any input you can provide on the following question! I'm... (2 Replies)
Discussion started by: Janus
2 Replies

10. Shell Programming and Scripting

put each word in new line - sed or tr

Hello ! I have a result of ls command in a file: file1 file2 file3.out file4.pdf file5 they all are separated by space. I need to put them on a separate line example: file1 file2 file3.out file4.pdf fil35 i tried sed 's/ /\n/g' inputfile > outputfile but did not help (3 Replies)
Discussion started by: hemangjani
3 Replies
Login or Register to Ask a Question