![]() |
|
|
grep unix.com with google
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Our Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
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
|
|
|||
|
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 |
|
|||
|
Quote:
tmp=tmp substr(str,i,1) |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
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 |