The UNIX and Linux Forums  


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



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: May 2009
Posts: 62
Reversing a line based on column

Hi,

I have a file that looks like this (tab seperated):


Code:
read   -  DFHJ
read1 -  IOPE
read2 + AAAB
read3 + MMMN

Basically what i want to do is reverse column 3 if column 2 has a - but leave it if its +

so the output would look like this:


Code:
read   -  JHFD
read1 -  EPOI
read2 + AAAB
read3 + MMMN

I am able to use awk to reverse individual lines but I cannot do it according to a particular column. thanks

  #2 (permalink)  
Old 11-30-2009
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 6,006

Code:
 awk 'function rev(str){ tmp=""; for(i=length(str);i>0;i--)
                                    {tmp=tmp substr(str,i,1)} 
                                 return tmp}
      {print $1, $2, ($2=="-") ? rev($3) : $3}  '  filename > newfilename

Bits Awarded / Charged to jim mcnamara for this Post
Date User Comment Amount
11-30-2009 rdcwayx nice function 100
  #3 (permalink)  
Old 11-30-2009
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,074
With Perl:


Code:
perl -ape'
  s/(-\s*)(\w+)/$1.reverse$2/e
  ' infile

  #4 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Jun 2006
Posts: 468
Quote:
Originally Posted by jim mcnamara View Post
Code:
 awk 'function rev(str){ tmp=""; for(i=length(str);i>0;i--)
                                    {tmp=tmp substr(str,i,1)} 
                                 return tmp}
      {print $1, $2, ($2=="-") ? rev($3) : $3}  '  filename > newfilename
nice function, especially this line:

tmp=tmp substr(str,i,1)
Sponsored Links
Reply

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How can i delete the duplicates based on one column of a line rdhanek Shell Programming and Scripting 11 08-05-2009 03:16 AM
Print entire line based on value in a column ppat7046 Shell Programming and Scripting 1 07-31-2009 03:20 PM
reversing a line kylle345 Shell Programming and Scripting 7 06-03-2009 07:34 AM
(cont) Retrieve line from a file based on a value in specific column efernandes UNIX for Dummies Questions & Answers 0 01-27-2007 01:00 PM
Retrieve line from a file based on a value in specific column efernandes UNIX for Dummies Questions & Answers 1 01-27-2007 11:04 AM



All times are GMT -4. The time now is 09:17 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