keep context in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting keep context in awk
# 1  
Old 05-23-2008
keep context in awk

here is a data file.
-------------------------------------
KSH, CSH, BASH, PERL, PHP, SED, AWK
KSH, CSH, BASH, PERL, PHP,
BASH, PERL, PHP, SED, AWK
CSH, BASH, PERL, PHP, SED,
KSH, CSH, BASH, PERL, PHP, SED, AWK
-------------------------------------

My desired output is
------------------------------------------------
KSH:Y CSH:Y BASH:Y PERL:Y PHP:Y SED:Y AWK:Y
KSH:Y CSH:Y BASH:Y PERL:Y PHP:Y SED:N AWK:N
KSH:N CSH:N BASH:Y PERL:Y PHP:Y SED:Y AWK:Y
KSH:N CSH:Y BASH:Y PERL:Y PHP:Y SED:Y AWK:N
KSH:Y CSH:Y BASH:Y PERL:Y PHP:Y SED:Y AWK:Y
------------------------------------------------

Can someone help!!
# 2  
Old 05-23-2008
Hm,
there's got to be a better way ...
(use nawk on Solaris)

Code:
awk 'BEGIN { FS = ", *" 
n = split("KSH, CSH, BASH, PERL, PHP, SED, AWK", _) }
{ for (j=1; j<=NF; j++) __[$j] 
  for (i=1; i<=n; i++) 
    $i = _[i] in __ ? _[i] ":Y" : _[i] ":N" 
  split("", __) # use delete __ if GNU Awk 
}1' input


But you should add Z-Shell, Python and Ruby Smilie

Last edited by radoulov; 05-23-2008 at 12:47 PM..
# 3  
Old 05-23-2008
Another approach:

Code:
awk 'BEGIN{FS=", ";OFS=" "
n=split("KSH, CSH, BASH, PERL, PHP, SED, AWK",a,FS)}
{ 
for(i=1;i<=n;i++) {
  s=match($0,a[i])?a[i]":Y ":a[i]":N "
  printf("%s",s)
}
print ""
}' file

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Questions related to if in awk context and if without awk context

I wrote this code, questions follow #! /bin/bash -f # Purpose - to show how if syntax is used within an awk clear; ls -l; echo "This will print out the first two columns of the inputted file in this directory"; echo "Enter filename found in this directory"; read input; ... (11 Replies)
Discussion started by: Seth
11 Replies

2. Shell Programming and Scripting

Context for use of [.symbol.] awk notation

Hi Just wondering ... do you have an example of context that would demonstrates how usefull the awk notation can efficiently be used ? Thx :rolleyes: (6 Replies)
Discussion started by: ctsgnb
6 Replies

3. UNIX for Advanced & Expert Users

Interupt Context Switching

If suppose a middle level interrupt is being serviced and a high priority interrupts comes in then in that case what all process will take place. The interrupt context switch will happen. But where will the interrupt context be saved? Is there something called as part process data area? (4 Replies)
Discussion started by: rupeshkp728
4 Replies

4. Slackware

Context dependent symlinks

Ive got multiple PCs, sharing an NFS mounted home dir. For certain apps I would like to keep the config files host specific. Easy solution is to create symlinks to local folders for configs. Ideally I would still want the .config files to reside in the user home folder. Is it possible to... (2 Replies)
Discussion started by: agentrnge
2 Replies

5. Homework & Coursework Questions

Context-switching - vmstat

1. The problem statement, all variables and given/known data: Type `vmstat -s; vmstat -n 1 5; vmstat -n 1 5; vmstat -s` to your Ruby interpreter. Then terminate your Ruby session. Run the Unix com- mand vmstat -s; vmstat -n 1 5; vmstat -s in the same terminal window you had been using for... (2 Replies)
Discussion started by: snowboarder
2 Replies

6. Homework & Coursework Questions

context-switching - vmstat

Hi all, I've got this question that i need to solve: "Type `vmstat -s; vmstat -n 1 5; vmstat -n 1 5; vmstat -s` to your Ruby interpreter. Then terminate your Ruby session. Run the Unix com- mand vmstat -s; vmstat -n 1 5; vmstat -s in the same terminal window you had been using for Ruby. Did... (1 Reply)
Discussion started by: snowboarder
1 Replies

7. UNIX for Dummies Questions & Answers

Context-switching question

Hi all, I've got this question that i need to solve: "Type `vmstat -s; vmstat -n 1 5; vmstat -n 1 5; vmstat -s` to your Ruby interpreter. Then terminate your Ruby session. Run the Unix com- mand vmstat -s; vmstat -n 1 5; vmstat -s in the same terminal window you had been using for Ruby. Did... (1 Reply)
Discussion started by: snowboarder
1 Replies

8. UNIX for Dummies Questions & Answers

Split based on Context

Hello, I have a file that I want to be able to split at specific positions. For example in the file below I want to be able to split at every occurence of 2 at the start of a line, into multiple files. 2 abc PQRST abcRSTG 2 cde FGKL abcLKGRG ABCLKgrg 2 lmn OPT lmopqrst uvwxyz ... (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

9. Shell Programming and Scripting

grep help after context

Hi, There's a file with below contents which I have to read based on the input parameter provided by the user. FILE_ID=1 FILE_FTP_ID=ftp.server1.com FILE_FTP_USER=user1 FILE_FTP_PASS=pass1 FILE_ID=2 FILE_FTP_ID=ftp.server2.com FILE_FTP_USER=user2 FILE_FTP_PASS=pass2 FILE_ID=3... (6 Replies)
Discussion started by: dips_ag
6 Replies

10. UNIX for Advanced & Expert Users

Context Switching

I know that this is a relative question but can someone give me an idea of what would be considered a high number of context switches? I am running vmstat and show a cs value of between 5000 and 6000 on a 4 processor system. How can I deduce if this number is high or not? Also, the timeslice... (2 Replies)
Discussion started by: keelba
2 Replies
Login or Register to Ask a Question