move the last word to begining of next line - SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting move the last word to begining of next line - SED
# 1  
Old 02-14-2008
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 -n -e 's/.*\(ListenPort="[0-9][0-9][0-9][0-9][0-9]"\)$/\1/p' test.txt

Input:
Code:
ListenAddress="xyz.com" ListenPort="11111"
Name="V1Server" NativeIOEnabled="true"
<Machine Name="xyz">
   <Manager ListenAddress="" ListenPort="11113" Name="xyz"/>
</Machine>

ListenAddress="xyz.com" ListenPort="11112"
Machine="xyz" Name="V2Server"

Expected Output:
Code:
ListenAddress="xyz.com" 
ListenPort="11111" Name="V1Server" NativeIOEnabled="true"
<Machine Name="xyz">
   <Manager ListenAddress="" ListenPort="11113" Name="xyz"/>
</Machine>
ListenAddress="xyz.com" 
ListenPort="11112" Machine="xyz" Name="V2Server"

Thanks

Last edited by Yogesh Sawant; 06-04-2010 at 04:04 AM.. Reason: added code tags
# 2  
Old 02-15-2008
This should give the expected ouput:

Code:
awk '/ListenAddress=/{printf("%s\n%s ", $1, $2);next}$0{print}' input

Regards
# 3  
Old 02-15-2008
Thanks for your reply

I still could not make it work due to syntax. Again, the pattern ListenAddress also appears in the middle of line which I dont want to tocuch it.
Code:
awk '/ListenAddress=/{printf("%s\n%s ", $1, $2);next}$0{print}' File1
awk: syntax error near line 1
awk: bailing out near line 1

Thanks again

Last edited by Yogesh Sawant; 06-04-2010 at 04:01 AM.. Reason: added code tags
# 4  
Old 02-15-2008
use nawk or /usr/xpg4/bin/awk for this syntax...
# 5  
Old 02-15-2008
Quote:
Originally Posted by baskar
Again, the pattern ListenAddress also appears in the middle of line which I dont want to tocuch it.
Sorry, to search for the pattern only on the beginning of the line:

Code:
awk '/^ListenAddress=/{printf("%s\n%s ", $1, $2);next}$0{print}' input

If you get errors, use gwk, nawk or /usr/xpg4/bin/awk on Solaris

Regards
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

sed: how to move matched pattern to end of previous line

Hello, I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help. I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 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

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

6. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: estebandido
3 Replies

7. Shell Programming and Scripting

Use sed to move last line to top

I have parsed a curl download with sed commands. I would also like to move the last line in the output file to the top. Can I use sed for this? (3 Replies)
Discussion started by: jostber
3 Replies

8. Shell Programming and Scripting

How to merge multiline into line begining with specific word

Hi, The file format is like the following. timestamp=2008-02-28-23.50.29.550675;category=CONTEXT;audit event=CONNECT; event correlator=2; database=CURDOMS;userid=inst3;authid=INST3; origin node=0;coordinator node=0; application id=AC122081.FA97.054468155029;application... (2 Replies)
Discussion started by: missyou
2 Replies

9. UNIX for Dummies Questions & Answers

how to move word by word on command line

Hey All, On commad promt of a shell.. How can we move our cursor word by word. Like Ctrl+A takes to the starting of the command... Any shortcut like that..? Thanks pbsrinivas (1 Reply)
Discussion started by: pbsrinivas
1 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