The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to replace the first word of a line if it occurs again in the next line(shell) geeko Shell Programming and Scripting 4 06-18-2009 03:36 PM
delete blank space in begining of line karthikn7974 Shell Programming and Scripting 4 05-07-2008 05:40 AM
Read line by line not word by word vadharah Shell Programming and Scripting 6 03-02-2008 02:29 PM
how to move word by word on command line pbsrinivas UNIX for Dummies Questions & Answers 1 11-23-2007 06:17 AM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 07:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-14-2008
baskar baskar is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 4
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:
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 Output:

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
  #2 (permalink)  
Old 02-15-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
This should give the expected ouput:


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

Regards
  #3 (permalink)  
Old 02-15-2008
baskar baskar is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 4
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.

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
  #4 (permalink)  
Old 02-15-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
use nawk or /usr/xpg4/bin/awk for this syntax...
  #5 (permalink)  
Old 02-15-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
Quote:
Originally Posted by baskar View Post
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0