Need to print last column based on the value of first 2 colums


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to print last column based on the value of first 2 colums
# 1  
Old 04-21-2010
Question Need to print last column based on the value of first 2 colums

Hi Everybody.

I need an urgent help, it would be great if somebody will help me out in this regard. See below sample file
Code:
 1525      805                  26                 2036   219644.  2598293.
 1525      805                 126                 2327   
 1525      805                 226                 2491   
 1525      805                 326                 2642   
 1525      805                 426                 2761   
 1525      805                 526                 2872
 1525      805                 626                 2961
 1525      805                 726                 3038
 1525      805                 826                 3116
 1525      805                 926                 3195
 1525      805                1026                 3269
 1525      805                1126                 3338
 1525      805                1226                 3409
 1525      805                1326                 3493
 1525      805                1426                 3577
 1525      805                1526                 3665
 1525      805                1626                 3763
 1525      805                1726                 3891
 1525      805                1826                 4000
 1525      805                1926                 4099
 1525      805                2026                 4198
 1525      805                2126                 4296
 1525      805                2226                 4394
 1525      805                2326                 4491
 1525      805                2426                 4587
 1525      805                2526                 4684
 1525      805                2626                 4781
 1525      805                2726                 4877
 1525      805                2826                 4974
 1525      825                  26                 2034   220065.  2598024.
 1525      825                 126                 2328
 1525      825                 226                 2491
 1525      825                 326                 2640
 1525      825                 426                 2761
 1525      825                 526                 2872
 1525      825                 626                 2961
 1525      825                 726                 3035
 1525      825                 826                 3112
 1525      825                 926                 3189
 1525      825                1026                 3261
 1525      825                1126                 3326


In the above file the last 2 cloumn values are dependent on 2nd column value, i.e whenever 2nd colum value is changed, the last 2 columns will print new values.I need an awk script that will print a file as below:

Code:
 1525      805                  26                 2036   219644.  2598293.
 1525      805                 126                 2327   219644.  2598293.
 1525      805                 226                 2491   219644.  2598293.
 1525      805                 326                 2642   219644.  2598293.
 1525      805                 426                 2761   219644.  2598293.
 1525      805                 526                 2872   219644.  2598293.
 1525      805                 626                 2961   219644.  2598293.
 1525      805                 726                 3038   219644.  2598293.
 1525      805                 826                 3116   219644.  2598293.
 1525      825                  26                 2034   220065.  2598024.
 1525      825                 126                 2328   220065.  2598024.
 1525      825                 226                 2491   220065.  2598024.

And So on upto the end of file.
I hope it clears my need.
Any help would be appreciated.

Regards

SA


Last edited by Franklin52; 04-21-2010 at 05:46 AM.. Reason: Please use code tags!
# 2  
Old 04-21-2010
Try this:

Code:
awk 'NR==1{print;l1=$5;l2=$6;prev=$2}NR>1{if ($2 == prev)print $0,l1,l2 ;else {l1=$5;l2=$6;prev=$2;print;next}}' file


cheers,
Devaraj Takhellambam
# 3  
Old 04-21-2010
Another approach:
Code:
awk '$2!=k{k=$2; s=$(NF-1) FS $NF; print; next} {print $0, s}' file

# 4  
Old 04-21-2010
Thank you very much for your prompt help.
Now my output file is as follows.

Code:
1525      805                  26                 2036   219644.  2598293.
 1525      805                 126                 2327 219644. 2598293.
 1525      805                 226                 2491 219644. 2598293.
 1525      805                 326                 2642 219644. 2598293.
 1525      805                 426                 2761 219644. 2598293.
 1525      805                 526                 2872 219644. 2598293.
 1525      805                 626                 2961 219644. 2598293.
 1525      805                 726                 3038 219644. 2598293.
 1525      805                 826                 3116 219644. 2598293.
 1525      805                 926                 3195 219644. 2598293.
 1525      805                1026                 3269 219644. 2598293.
 1525      805                1126                 3338 219644. 2598293.
 1525      805                1226                 3409 219644. 2598293.
 1525      805                1326                 3493 219644. 2598293.
 1525      805                1426                 3577 219644. 2598293.
 1525      805                1526                 3665 219644. 2598293.
 1525      805                1626                 3763 219644. 2598293.
 1525      805                1726                 3891 219644. 2598293.
 1525      805                1826                 4000 219644. 2598293.
 1525      805                1926                 4099 219644. 2598293.
 1525      805                2026                 4198 219644. 2598293.
 1525      805                2126                 4296 219644. 2598293.
 1525      805                2226                 4394 219644. 2598293.
 1525      805                2326                 4491 219644. 2598293.
 1525      805                2426                 4587 219644. 2598293.
 1525      805                2526                 4684 219644. 2598293.
 1525      805                2626                 4781 219644. 2598293.
 1525      805                2726                 4877 219644. 2598293.
 1525      805                2826                 4974 219644. 2598293.
 1525      825                  26                 2034   220065.  2598024.

The last column is not justified, can you please help me to enhance your command to get last 2 column justified output file.

Once again thank you very much for your help.

Last edited by vgersh99; 04-21-2010 at 06:44 AM.. Reason: code tags, please!
# 5  
Old 04-21-2010
Try this...

Code:
awk '$5$6{m=$5"\t"$6} {print $1,$2,$3,$4,m}' OFS="\t" infile

# 6  
Old 04-21-2010
It seems as if you effectively want to print all lines with 6 fields and skip lines with 4 fields. If this is true then you can simply use:
Code:
awk 'NF==6' infile

# 7  
Old 04-21-2010
To keep the format you can try this:
Code:
awk '{gsub(/[ \t]+$/, "", $0)}
$2!=k {
  k=$2
  s=sprintf("%9s%10s",$(NF-1), $NF)
  print
  next
} 
{print $0, s}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print column based on pattern

Hi all, how print on columns when contain un pattern specific, e.g. $cat file1 3234 234 2323 number1 number2 number3 123 242 124 124 number2 324 424 543 626 number1 3463 234 534 345 number3 6756 345 2352 334 345 234 need output file1 way (2 Replies)
Discussion started by: aav1307
2 Replies

2. Shell Programming and Scripting

Print the column content based on the header

i have a input of csv file as below but the sequence of column get changed. I,e it is not necessary that name comes first then age and rest all, it may vary. name,age,marks,roll,section kevin,25,80,456,A Satch,23,56,789,B Meena,24,78,H245,C So i want to print that column entires which... (12 Replies)
Discussion started by: millan
12 Replies

3. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

4. Solaris

awk - Print variable number of colums from a starting column

Hi guys, I usualy am able to google awk stuff but I can't find it so far and there are so many awking gurus here that I will give it a shot. I want to print $1;$3;"$5 up to the $NF". In other words, I can have 1000 colums, but need to have $5 up to the end. I started with the idea of... (2 Replies)
Discussion started by: plmachiavel
2 Replies

5. Shell Programming and Scripting

awk print non matching lines based on column

My item was not answered on previous thread as code given did not work I wanted to print records from file2 where comparing column 1 and 16 for both files find rows where column 16 in file 1 does not match column 16 in file 2 Here was CODE give to issue ~/unix.com$ cat f1... (0 Replies)
Discussion started by: sigh2010
0 Replies

6. Shell Programming and Scripting

Q: print a column based on the uniqe value of another

Hi there, If I have the following: and want to print the first column based on the uniqe value of the second column, so the output would be something like: how would I do that, using AWK and the piping technique? Thanks in advance (12 Replies)
Discussion started by: Abdulelah
12 Replies

7. Shell Programming and Scripting

How to print column based on row number

Hi, I want to print column value based on row number say multiple of 8. Input file: line 1 67 34 line 2 45 57 . . . . . . line 8 12 46 . . . . . . line 16 24 90 . . . . . . line 24 49 67 Output 46 90 67 (2 Replies)
Discussion started by: Surabhi_so_mh
2 Replies

8. Shell Programming and Scripting

Filter the column and print the result based on condition

Hi all This is my output of the some SQL Query TABLESPACE_NAME FILE_NAME TOTALSPACE FREESPACE USEDSPACE Free ------------------------- ------------------------------------------------------- ---------- --------- ---------... (2 Replies)
Discussion started by: jhon
2 Replies

9. Shell Programming and Scripting

Print entire line based on value in a column

Friends, File1.txt abc|0|xyz 123|129|opq def|0|678 890|pqw|sdf How do I print the entire line where second column has value is 0? Expected Result: abc|0|xyz def|0|678 Thanks, Prashant ---------- Post updated at 02:14 PM ---------- Previous update was at 02:06 PM ---------- ... (1 Reply)
Discussion started by: ppat7046
1 Replies

10. Shell Programming and Scripting

Sorting a flat file based on multiple colums(using character position)

Hi, I have an urgent task here. I am required to sort a flat file based on multiple columns which are based on the character position in that line. I am restricted to use the character position instead of the space and sort +1 +2 etc to do the sorting. I understand that there is a previous... (8 Replies)
Discussion started by: cucubird
8 Replies
Login or Register to Ask a Question