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-27-2009
Registered User
 

Join Date: Oct 2009
Posts: 18
output - tab formatted - awk

Dear All,

Good Day. I would like to hear your suggestions for the following problem:

I have a file with 5 columns with some numbers in 16 lines as shown below.
Input file:

Code:
Col 1    Col 2   Col 3    Col 4     Col 5
  12      220        2      121        20
 234       30      22         9       156
  25      129    320        94          7
.
.
.
.

I need to do the following


Code:
Col_1new= Col1 ~ Col2   (# difference between Col1 and Col2)
Col_2new= Col2 ~ Col3
Col_3new= Col3 ~ Col4

and so on.

I need the output will be like this


Code:
Col_1new   Col_2new   Col_3new   Col_4new
    208           218          119           101

and so on. The output should be in the column format. I am looking for an awk program to do the same. If anyone help me in this regard.

Thanks in advance.

Warm regards
Fredrick.

Last edited by radoulov; 11-27-2009 at 07:59 AM.. Reason: Use code tags, please!
  #2 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Jun 2006
Posts: 468

Code:
awk 'BEGIN{print "Col_1new Col_2new Col_3new Col_4new"}
NR>1 {print $2-$1,$3-$2,$4-$3,$5-$4}' urfile

or


Code:
awk 'BEGIN{print "Col_1new Col_2new Col_3new Col_4new"}
NR>1 {printf "%s\t%s\t%s\t%s\n", $2-$1,$3-$2,$4-$3,$5-$4}' urfile |sed 's/-//g'

  #3 (permalink)  
Old 11-27-2009
radoulov's Avatar
--
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 3,074
Assuming the header lines are only for clarity:


Code:
awk '{ 
  for (i=1; i<NF; i++) 
    printf "%s", abs($i - $(i+1)) \
      (i == NF - 1 ? RS : "\t")
    }
  func abs(x) { return x < 0 ? -x : x }
  ' infile

Otherwise:


Code:
awk 'NR == 1 { 
  for (i=1; i<NF; i++) 
    printf "Col_%dnew%s", i, i == NF - 1 ? RS : "\t"
	next 
	}
{ 
  for (i=1; i<NF; i++) 
    printf "%s", abs($i - $(i+1)) (i == NF - 1 ? RS : "\t")
	}
  func abs(x) { return x < 0 ? -x : x }
  ' infile

Bits Awarded / Charged to radoulov for this Post
Date User Comment Amount
11-27-2009 danmero x < 0 ? -x : x 1,000
  #4 (permalink)  
Old 11-27-2009
danmero danmero is offline Forum Advisor  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,517

Code:
awk 'NR > 1 { for(i = 0; ++i < NF;){x = ($i - $(i+1)) ; x = x < 0 ? -x : x ; r = r ? r "\t" x : x} print r ; r = ""}' infile

Sponsored Links
Bits Awarded / Charged to danmero for this Post
Date User Comment Amount
11-27-2009 radoulov i = 0; ++i < NF; 1,000
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
cut - columns with formatted Output dhanamurthy Shell Programming and Scripting 9 05-19-2008 11:20 AM
Formatted Output dhanamurthy Shell Programming and Scripting 6 05-13-2008 03:30 AM
Formatted output - awk dhanamurthy Shell Programming and Scripting 3 05-12-2008 12:25 AM
formatted output with commas joeyg Shell Programming and Scripting 4 03-04-2008 03:54 PM
Formatted output in KSH psynaps3 Shell Programming and Scripting 1 07-05-2006 09:03 AM



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