Using awk/sed to determine whether there are any 2's, 3's and 4's in a column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using awk/sed to determine whether there are any 2's, 3's and 4's in a column
# 1  
Old 11-02-2011
Using awk/sed to determine whether there are any 2's, 3's and 4's in a column

Hi,

I have a very large file which is of the general format:

0 3 4 2 ...
3 2 4 0 ...
0 3 4 2 ...
3 0 4 2 ...
. . . . ...
. . . . ...

I would like to apply a simple awk/sed script to work out whether there are any 2's, 3's and 4's in each column.

The results would look as follows (where 1 indicates that there is at least one occurrence of the value (2,3 or 4 respectively) and a 0 indicates that there is no occurrence of the value (2,3,or 4 respectively)):

0 1 0 1
1 1 0 0
0 0 1 0

(ie: column 1 has no 2's, at least one 3 and no 4's;
column 2 has at least one 2, at least one 3 and no 4's;
column 3 has no 2's, no 3's and at least one 4;
column 4 has at least one 2, no 3's and no 4's etc...)

Many thanks in advance,
Kathryn
# 2  
Old 11-02-2011
A simple sed substitute will work
Code:
sed "s#[1-4]#1#g" input > output

Will replace any number from 1 to 4 with 1 globally.
# 3  
Old 11-02-2011
Thanks for that. Sorry though I wasn't entirely clear about what I was after.

I've extended the size of my file as below to make it clearer:

0 3 4 2 0 2 2 4...
3 2 4 0 0 4 2 2...
0 3 4 2 2 3 4 0...
3 0 4 2 0 3 2 4...
. . . . ...
. . . . ...

What I would like is the following output:

0 1 0 1 1 1 1 ...
1 1 0 0 0 1 0 ...
0 0 1 0 0 1 1 ...

where the first row indicates whether the column has a 2 or not, the second row indicates whether the column has a 3 or not and the third row indicates whether the column has a 4 or not.


Again, many thanks in advance for your help,
Kathryn
# 4  
Old 11-03-2011
I can't figure out the input / output relation but...

This awk should work :
Code:
$ cat input
0 3 4
2 3 4
0 0 4 5
$ cat output
0 1 1
1 1 1
0 0 1 1
$ awk '{ for (i=1;i <= NF; i++) if ( $i == i+1) sub(i+1,1,$i) }1 ' input
0 1 1
1 1 1
0 0 1 1

Hope that helps.
Regards
Peasant.
 
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/sed summation of one column based on some entry in first column

Hi All , I am having an input file as stated below Input file 6 ddk/djhdj/djhdj/Q 10 0.5 dhd/jdjd.djd.nd/QB 01 0.5 hdhd/jd/jd/jdj/Q 10 0.5 512 hd/hdh/gdh/Q 01 0.5 jdjd/jd/ud/j/QB 10 0.5 HD/jsj/djd/Q 01 0.5 71 hdh/jjd/dj/jd/Q 10 0.5 ... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

3. Shell Programming and Scripting

How to determine column separation format?

Hi there, say I have a line with multiple columns but with different separation formats: spaces, tabs.. Is it possible to have AWK print the separation format between each column? (10 Replies)
Discussion started by: la2015
10 Replies

4. Shell Programming and Scripting

Using sed in awk column

Hello, I need to translate a file with more thousand lines like thiszoneobjectrename "AAA_to_BBB"in a lineszoneobjectrename "AAA_to_BBB","AAA_BBB" I tried to using awk and sed in this manner but it don't workgrep _to_ Documenti/zoneSWITCH2 |awk '{print "zoneobjectrename " $2 "," $2= sed... (7 Replies)
Discussion started by: alen192
7 Replies

5. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
3 Replies

6. Shell Programming and Scripting

use sed on only column $3 with awk

I am using the command ls -lhB --group-directories-first --no-group --file-type --time-style=+%D $@ | sed 's/AlphaLexman/ALM/g' to replace my username with my initials (to keep the width shorter), but if my username is in the filename such asreport_by_AlphaLexman.txtit becomesreport_by_ALM.txt ... (2 Replies)
Discussion started by: AlphaLexman
2 Replies

7. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

8. Shell Programming and Scripting

Deleting the first column with sed,awk or perl

336 brtr 256 hello Output: brtr hello How can i do this with sed,awk or perl? (5 Replies)
Discussion started by: cola
5 Replies

9. Shell Programming and Scripting

awk or sed to determine battery percentage

So, I know this can be done but my awk and sed wizardry is not up to snuff for the job. Basically, I have tons and tons of Macbooks at my work and I am trying to grab the current percentage of the battery power on each client. If I use the system_profiler SPPowerDataType command it prints off... (4 Replies)
Discussion started by: tlarkin
4 Replies

10. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies
Login or Register to Ask a Question