output - tab formatted - awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting output - tab formatted - awk
# 1  
Old 11-27-2009
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.Smilie

Last edited by radoulov; 11-27-2009 at 07:59 AM.. Reason: Use code tags, please!
# 2  
Old 11-27-2009
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  
Old 11-27-2009
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

# 4  
Old 11-27-2009
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk printing leading tab in output

The awk below executes and produces the current output. it skips the header in row 1 and prints $4,$5,$6 and then adds the header row back. The problem is that it keeps the tailing tab and prints it in front of $1. I could add a pipe to remove the tab, but is there a better way to do it with on... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

awk to add tab to output of both conditions

In the below awk written by @RavinderSingh13 I have added a few lines and am trying to have the output be tab-delimited. The input is space-delimeted and the portion in bold seems to add a tab to the Not found but not the found. Thank you :). file1 One 1 Two 2 Three 3 file2 One 1... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk output is space delimeted not tab delimeted

In the below awk the output is space delimited, but it should be tab delimited. Did I not add the correct -F and OFS? Thank you :). The input file are rather large so I did not include them, but they are tab-delimeted files as well. awk awk -F'\t' -v OFS='\t' 'FNR==1 { next } > ... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk output seperated by tab

I am just trying to output the below awk separated by tabs. Thank you :). awk (added OFS as an attempt to itroduce tabs) awk '{split($5,a,"-"); OFS='\t' print $1,$2,$3,a}' file.bed > test.bed The awk runs and produces all the data in 1 field instead of 4 fields. current output ... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Programming

Formatted output in PERL

Hi Techies, I'm a newbie to PERL, Please help me with following problem. I have an input text file like below cat Input.txt 418673132,P 492538858,P 384535478,P 521522357,I 529435679,I 183617024,P 184414408,I 735510689,P 736238343,I 411642045,I 412690979,I 104232783,I (2 Replies)
Discussion started by: mahi_mayu069
2 Replies

6. Shell Programming and Scripting

Problems with awk printf, formatted output

Hi, i have a script, which is incomplete, am on my way developing it. Input 1,12,2012,IF_TB001 2,12,2012,3K3 3,Z56,00000,25,229,K900,00, ,3G3, ,USD, ,0000000000,000, , , , 550000000 3,Z56,00000,53,411,W225,00,000, , ,USD,OM170,0000000000,000, , , , -550000000 4,Z56,COUNT, 4,SUM LOC,... (19 Replies)
Discussion started by: selvankj
19 Replies

7. UNIX for Dummies Questions & Answers

Request for Formatted Output

Can you please tell me how to just get only the output of dealers I & V information along with their subtotals in the next line of the file and create a new file, The dealer position along with corresponding totals may change everyday to any position above or below in the file, please help Thanks (2 Replies)
Discussion started by: Ariean
2 Replies

8. Shell Programming and Scripting

Formatted Output

Hi I have the following lines in a file SWPRC000001NOT STATED 1344 SWPRC000001NOT STATED 1362 SWPRC000001NOT STATED 1418 SWPRC000001NOT STATED 1436 SWPRC000001NOT STATED ... (6 Replies)
Discussion started by: dhanamurthy
6 Replies

9. Shell Programming and Scripting

Formatted output - awk

Hi I have the following records in a file SABN YOURTUBE 000514 7256 SACN XYOUDSDF 000514 7356 SADN KEHLHRSER 000514 7656 SAEN YOURTUBE 000514 7156 SAFN YOURTUBE 000514 7056 I need to put this in the format like this printf '%s %-50s %6s %-6s\n' I am not going to read individual... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

10. Shell Programming and Scripting

Formatted output in KSH

Hi, Is there some way to get formatted output in ksh? Something like a properly alligned tabular format. I tried adding '\t' to echo statements, but it doesn't come properly alligned 'hello' A simple Hello 'helloworld' A helloworld statement I need the second coloumn to... (1 Reply)
Discussion started by: psynaps3
1 Replies
Login or Register to Ask a Question