The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
run script with ./ in front of name rig123 Shell Programming and Scripting 3 03-10-2006 04:58 AM
Appending columns on a file abel Shell Programming and Scripting 2 09-27-2002 07:04 AM
Front end on Unix shibz UNIX for Advanced & Expert Users 4 11-17-2001 12:37 PM

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 07-08-2008
ragavhere ragavhere is offline
Registered User
  
 

Join Date: Apr 2008
Location: Chennai,India
Posts: 79
Red face Appending the last few columns to the front

Hi

consider this as the first line
00010015 MORGAN STANLEY & CO INCORPORATED N 110 INVESTAR 1 0001OT NJ 201-830-5055 01-Jan-1974 00:00:00 1 01-May-2008 00:00:00 05-Jun-2008 13:34:18 0001 - From SMSRun1_GIDQA02
Consider this as the second line
00010015 MORGAN STANLEY & CO INCORPORATED N 110 INVESTAR 1 0001OT NJ 201-830-5055 01-Jan-1974 00:00:00 1 01-May-2008 00:00:00 05-Jun-2008 13:34:18 0001 egesdegjeog rogjeogje - From SMSRun1_GIDQA02

Here i need to cut "- From SMSRun1_GIDQA02" from the end of each line and need to append it at the front as "From SMSRun1_GIDQA02 - ". So that the output looks like

From SMSRun1_GIDQA02 - 00010015 MORGAN STANLEY & CO INCORPORATED N 110 INVESTAR 1 0001OT NJ 201-830-5055 01-Jan-1974 00:00:00 1 01-May-2008 00:00:00 05-Jun-2008 13:34:18 0001

But the problem here is that the position of "- From SMSRun1_GIDQA02" might vary from line to line since there might be odd number of columns.Can "- From" be taken as a key to cut "- From SMSRun1_GIDQA02" and append it to the front?

Last edited by ragavhere; 07-08-2008 at 06:49 AM.. Reason: Line
  #2 (permalink)  
Old 07-08-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,305
With sed:

Code:
sed 's/\(.*\)\( - \)\(.*\)/\3\2\1/' file > newfile
Regards
  #3 (permalink)  
Old 07-08-2008
ragavhere ragavhere is offline
Registered User
  
 

Join Date: Apr 2008
Location: Chennai,India
Posts: 79
Thanks. Can you please explain it?

Regards,

Ragav.

Last edited by ragavhere; 07-08-2008 at 07:41 AM.. Reason: Explanation
  #4 (permalink)  
Old 07-08-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,305
Quote:
Originally Posted by ragavhere View Post
Thanks. Can you please explain it?

Regards,

Ragav.
Code:
 sed 's/\(.*\)\( - \)\(.*\)/\3\2\1/' file > newfile
With \(.*\) you can select portions of a string. This portion is recalled in the replacement string with \1, \2, \3 etc.

The first portion is the begin of the string until the space before the last minus sign (before " - From").
The second portion are the 3 characters " - " before "From" and the last portion is the rest of the line.
In the replacement string the portion is recalled in the order \3\2\1.

Have a read of a sed tutorial, you can find some links here:

Unix Tutorials/Programming Tutorials/Shell Scripting Tutorials

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 09:57 PM.


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