Visit Our UNIX and Linux User Community


Format output in AWK command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format output in AWK command
# 1  
Old 09-27-2012
Question Format output in AWK command

hi Friends ,

I have a file as below

s.txt
Code:
1~2~~4
2~6~~7
3~8~~9

t.txt
Code:
1~2~~4
2~5~8~7
3~8~~7

header for both files is common

header.txt
Code:
KEY~DIS~NOT~KAT

to compare 2 files i have used the following code

Code:
paste -d '~' s.txt t.txt | awk -F '~' '{c=NF/2;for(i=1;i<=c;i++)if($i!=$(i+c))printf "line %-5s field %s orginal %s changed  %s\n",NR,i-1,$i,$(i+c)}'

I got output as

Actualoutput.txt
Code:
line 2     field 1 orginal 6 changed  5
line 2     field 2 orginal  changed  8
line 3     field 3 orginal 9 changed  7


But i am unable to find the solution to make my script inculde the header information in the output file. Plz help me. your help is much appreciated.

ExpectedOutput.txt
Code:
KEY~COLUMNNAME~SOURCEVALUE~TARGETVALUE
2  ~ DIS      ~   6       ~    5
2  ~ NOT      ~          ~     8
3  ~ KAT      ~    9     ~     7

# 2  
Old 09-27-2012
Assuming only 1 line in header.txt, try:
Code:
awk -F'~' -v header="$(<header.txt)" 'BEGIN{OFS=" ~ ";split(header,h)
print "KEY","COLUMNNAME","SOURCEVALUE","TARGETVALUE"}
FNR==NR{for(i=1;i<=NF;i++) e[FNR,i]=$i;next}
{for(i=1;i<=NF;i++) 
  if($i!=e[FNR,i])
   print FNR,h[i],e[FNR,i],$i
}' s.txt t.txt

# 3  
Old 09-27-2012
@Elixir . Thanks for the reply. Smilie you made my day. Smilie it is working as Expected.

Previous Thread | Next Thread
Test Your Knowledge in Computers #801
Difficulty: Medium
The CSS z-index property specifies the stack order of an element.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Script to format output

Hi all, I'm fairly new to this and learning along the way, so bare with me... I'm trying to format the output from a script to be more read-friendly. The output contains more servers and more processes but for as example it will do: Server: Test1 ... (4 Replies)
Discussion started by: Bobsonm
4 Replies

2. Shell Programming and Scripting

Format output using awk

Hello all , need help with this ... Input File DEV % POOL 0CB4 FBA 2211300 81792 4 IE RAID-5(3+1) R5_EFD100_1 - - 1805376 82 IF RAID-1 M2_FC300_1 - ... (4 Replies)
Discussion started by: greycells
4 Replies

3. Shell Programming and Scripting

How to format output of command?

Hi how do you change the format of the output of ls -lt -c -R $HOME | sed /^total/d From: /home/pikamon/Desktop: -rwxr-xr-x 1 pikamon pikamon 35 Sep 18 14:25 fileModified.sh -rwxr-xr-x 1 pikamon pikamon 87 Sep 18 14:25 fileModified.sh~ /home/darksky21/Downloads: -rwxrw-rw- 1 pikamon... (5 Replies)
Discussion started by: pikamon
5 Replies

4. UNIX for Dummies Questions & Answers

after awk-> format output

hi i have a awk command with several querys.... awk 'FS="|""; print $4, $5, $6...etc.... $4 gives me the date 20120304 $5 is timestamp 101023 I want to format these in 2012.03.04 or 2012/03/04 10:10:23 but have no idea, if this is possible with format-parameters in the awk... (2 Replies)
Discussion started by: Jazzmatazz
2 Replies

5. Shell Programming and Scripting

awk to format an output

awk experts, I have in put file with time stamp followed by "," separated data. same patern continues. The output need time stamp in first columns and data total in 2nd columns. Input file T 9:15 d0,1,3,3 d1,2,1,1 d2,3,1,5 e1,1,1,1 T 9:30 d0,1,1,1 d1,2,3,2 d3,1,2,1... (10 Replies)
Discussion started by: arv_cds
10 Replies

6. Shell Programming and Scripting

awk - format output

Input file1 zone: BAU_SERVER1 C0:50:76:01:C6:20:00:12; 50:06:01:69:3B:20:14:8B; 50:06:01:60:3B:20:14:8B zone: BAU_SERVER2 C0:50:76:01:C6:20:00:08; 50:06:01:69:3B:20:14:8B; 50:06:01:60:3B:20:14:8B zone: ... (4 Replies)
Discussion started by: greycells
4 Replies

7. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

8. Shell Programming and Scripting

Format Output with AWK command

Hi - I have a file with contents as below. 12.1 a.txt 12.1 b.txt 12.1 c.txt 13.2 a.txt 13.2 d.txt 14.3 f.txt 15.4 a.txt 15.4 b.txt 15.4 z.txt I need to print the contents like this. 12.1 a.txt <&nbsp><&nbsp><&nbsp>b.txt <&nbsp><&nbsp><&nbsp>c.txt (7 Replies)
Discussion started by: guruparan18
7 Replies

9. Shell Programming and Scripting

[need help] output format from awk

hi all, i have a problem with my nawk command output below is the description : nawk $12 == "00008001" { cnt++;cs_cd } END {for(cd in cs_cd) print cd, cs_cd } 2007020814.TDR output : 133 123 desire output: 133,123,.... please advices thank you so much (6 Replies)
Discussion started by: bucci
6 Replies

10. Shell Programming and Scripting

Output in a particular format using AWK

Hi All, I am trying to check if if column 5 is greater than 90. If greater it will print the term in column 6, else if all are within limit, then it will output "Size is within limit". I can't seem to do that with the below code. The output should only be 1 statement of "Size is within the... (4 Replies)
Discussion started by: Raynon
4 Replies

Featured Tech Videos