key value : awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting key value : awk
# 1  
Old 06-18-2008
key value : awk

Input file:

Code:
x=1
y=2
z=5


Is it possible using awk or anything to get the following output?

Code:
key=x|y|z
value=1|2|5

I can use two separate code lines using awk or cut (with tr) to get the key and value separately, was wondering if this can be done with some awk code at a once.

Thanks
# 2  
Old 06-18-2008
Something like this?

Code:
awk -F = '{ key[++i] = $1; value[i] = $2 }
END {
  printf "key"; sep="="; for (j=1; j<=i; ++j) { printf ("%s%s", sep, key[j]); sep="|" }; printf "\n";
  printf "value"; sep="="; for (j=1; j<=i; ++j) { printf ("%s%s", sep, value[j]); sep="|" }; printf "\n";
}' file

If NR is defined in the END block in your variant of awk, you can replace the variable i with NR (and of course incrementing it is then done implicitly).
# 3  
Old 06-18-2008
Thank you era. Ya this worked
# 4  
Old 06-18-2008
Or:
(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk -F= 'END { printf "keys=%s\nvalues=%s\n", k, v }
{ k = k ? k s $1 : $1; v = v ? v s $2 : $2 }
' s="|" file

The same with perl:

Code:
perl -F"=" -lane'
  $k.=$F[0]."|";
  $v.=$F[1]."|";
  END {
    chop($k,$v);  
    printf "keys=%s\nvalues=%s\n",$k,$v
	}'  file


Last edited by radoulov; 06-18-2008 at 07:30 AM..
# 5  
Old 06-18-2008
Or how about

Code:
perl -F= -lane 'push @k, shift @F; push @v, @F;
END { print "keys=", join ("|", @k), "\nvalues=", join ("|", @v) }' file

# 6  
Old 06-18-2008
Quote:
Originally Posted by era
Or how about

Code:
perl -F= -lane 'push @k, shift @F; push @v, @F;
END { print "keys=", join ("|", @k), "\nvalues=", join ("|", @v) }' file

Nice Smilie

With zsh:

Code:
zsh-4.3.4% printf "keys=%s\nvalues=%s\n" ${(j:|:)$(<file)%\=*} ${(j:|:)$(<file)#*=}
keys=x|y|z
values=1|2|5


Last edited by radoulov; 06-18-2008 at 10:06 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Files summary using awk based on index key

Hello , I have several files which are looking similar to : file01.txt keyA001 350 X string001 value001 keyA001 450 X string002 value007 keyA001 454 X string002 value004 keyA001 500 X string003 value005 keyA001 255 X string004 value006 keyA001 388 X string005 value008 keyA001 1278 X... (4 Replies)
Discussion started by: alex2005
4 Replies

2. Shell Programming and Scripting

awk - Merge two files based on one key

Hi, I am struggling with the an awk command to merge two files based on a common key. I want to append the value from File2 ($2) onto the end of File1 where $1 from each file matches - If no match then nothing is apended File1 COL1|COL2|COL3|COL4|COL5|COL6|COL7... (3 Replies)
Discussion started by: Ads89
3 Replies

3. UNIX for Dummies Questions & Answers

awk - Print lines if only matching key is found

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. Thanks a lot. Any help is appreciated. Script I am using: awk 'FNR == NR && ! /^]*$/ {... (9 Replies)
Discussion started by: High-T
9 Replies

4. Shell Programming and Scripting

Extracting key/value pairs in awk

I am extracting a number of key/value pairs in awk using following: awk ' /xyz_session_id/ { n=index($0,"xyz_session_id"); id=substr($0,n+15,25); a=$4; } END{ for (ix in a) { print a } }' I don't like this Index + substr with manually calculated... (5 Replies)
Discussion started by: migurus
5 Replies

5. Shell Programming and Scripting

Merge multiple lines in same file with common key using awk

I've been a Unix admin for nearly 30 years and never learned AWK. I've seen several similar posts here, but haven't been able to adapt the answers to my situation. AWK is so damn cryptic! ;) I have a single file with ~900 lines (CSV list). Each line starts with an ID, but with different stuff... (6 Replies)
Discussion started by: protosd
6 Replies

6. Shell Programming and Scripting

Using AWK to format output based on key field

I have file which contains gene lines something like this Transcript Name GO POPTR_0016s06290.1 98654 POPTR_2158s00200.1 11324 POPTR_0004s22390.1 12897 POPTR_0001s11490.1 POPTR_0016s13950.1 14532 POPTR_0015s05840.1 13455 POPTR_0013s06470.1 12344... (6 Replies)
Discussion started by: shen
6 Replies

7. Shell Programming and Scripting

Gawk / Awk Merge Lines based on Key

Hi Guys, After windows died on my netbook I installed Lubuntu and discovered Gawk about a month ago. After using Excel for 10+ years I'm amazed how quick and easily Gawk can process data but I'm stuck with a little problem merging data from multiple lines. I'm an SEO Consultant and provide... (9 Replies)
Discussion started by: Jamesfirst
9 Replies

8. Shell Programming and Scripting

Print a key with its all values using awk/others

input COL1 a1 b1 c1 d1 e1 f1 C1 10 10 10 100 100 1000 C2 20 20 200 200 200 2000 output C1 a1 10 1 C1 b1 10 1 C1 c1 10 1 C1 d1 100 2 C1 e1 100 2 C1 f1 1000 3 C2 ... (12 Replies)
Discussion started by: ruby_sgp
12 Replies

9. Shell Programming and Scripting

Compare 2 huge files wrt to a key using awk

Hi Folks, I need to compare two very huge file ( i.e the files would contain a minimum of 70k records each) using awk or sed. The comparison needs to be done with respect to a 'key'. For example : File1 ********** 1234|TONY|Y75634|20/07/2008 1235|TINA|XCVB56|30/07/2009... (13 Replies)
Discussion started by: Ranjani
13 Replies
Login or Register to Ask a Question