gensub and arraywith awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers gensub and arraywith awk
# 1  
Old 04-14-2012
gensub and arraywith awk

Hi Unix.com !

I would need some help for something I don't understand Smilie

input:
Code:
111|2 Y Z blue.
333|4 W X blue.; 5 Y Z red.
666|7 W X red.; 8 Y Z blue.
999|10 U V red.; 11 W X blue.; 12 Y Z red.

From $2, I would like to remove the sub-strings containing "blue" (and the associated "; " before or after), and print the new strings in $3, in order to obtain this output:
Code:
111|2 Y Z blue.|
333|4 W X blue.; 5 Y Z red.|5 Y Z red.
666|7 W X red.; 8 Y Z blue.|7 W X red.
999|10 U V red.; 11 W X blue.; 12 Y Z red.|10 U V red.; 12 Y Z red.

I tried:
Code:
BEGIN{FS=OFS="|"}

NR==1{
          $3 = "Field3"
          }
NR>1{

    $3 = $2
	
    y = split($3,x,"; ")
        for (i=1; i<=y; i++){
        	
            if (x[i] ~ /blue/ && y=1){
            	$3 = gensub(x[i],"","g",$3)
        	}
        	
            else if (x[i] ~ /blue/ && i<y)
            	$3 = gensub(x[i]"; ","","g",$3)
            
            else if (x[i] ~ /blue/ && i=y)
                $3 = gensub("; "x[i],"","g",$3)
        }
        
    
}1

I cannot get rid of the "; " !

Code:
Field1|Field2|Field3
111|2 Y Z blue.|
333|4 W X blue.; 5 Y Z red.|; 5 Y Z red.
666|7 W X red.; 8 Y Z blue.|7 W X red.; 
999|10 U V red.; 11 W X blue.; 12 Y Z red.|10 U V red.; ; 12 Y Z red.

I tried to write the regexp with slashes, double quotes, braces, brackets in every single positions ... always the same results.

HELP !!!!

Last edited by beca123456; 04-14-2012 at 05:47 AM..
# 2  
Old 04-14-2012
Hi beca123456,

One way:
Code:
$ cat infile
111|2 Y Z blue.
333|4 W X blue.; 5 Y Z red.
666|7 W X red.; 8 Y Z blue.
999|10 U V red.; 11 W X blue.; 12 Y Z red.
$ cat script.awk
BEGIN {
        FS = OFS = "|";
}

{
        split( $2, f, /;/ );
        for ( i = 1; i <= length( f ); i++ ) {
                if ( match( f[i], /blue/ ) >  0 ) {
                        continue;
                }
                $3 = $3 ( $3 ? ";" : "" ) f[i];
        }

        print $0;
}
$ awk -f script.awk infile
111|2 Y Z blue.
333|4 W X blue.; 5 Y Z red.| 5 Y Z red.
666|7 W X red.; 8 Y Z blue.|7 W X red.
999|10 U V red.; 11 W X blue.; 12 Y Z red.|10 U V red.; 12 Y Z red.

This User Gave Thanks to birei For This Post:
# 3  
Old 04-14-2012
Thanks birei, works great !

Although, I have a question. I am not sure I understand this part:
Code:
$3 = $3 ( $3 ? ";" : "" ) f[i]

Tell me if I am wrong (I make it matches with colour):
Code:
$3 = $3 ( $3 ? ";" : "" ) f[i]

If the condition above is true, then in $3 print the modified field 3 if it exists, followed by ";" or nothing depending if it is the end of the field or not ?
So in this case, why do you add f[i] at the end ?

Does ":" stand for a logical OR?
# 4  
Old 04-15-2012
f[i] gets added because that needed to get added in the first place, the part in parentheses is just to determine if a ";" should be put in between.

Alternative version:
Code:
awk -F'[|]|; ' '{s=""; for(i=2;i<=NF;i++) if($i!~/blue/) s=s (s?"; ":"") $i; print $0"|"s}' infile


Last edited by Scrutinizer; 04-15-2012 at 01:43 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-15-2012
Thanks Scrutinizer !

I get it now.
 
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: split and gensub query

Hi All, Thanks for answering my previous question. Could you please explain the highlighted code? awk -v pos='9 27 39 54 59 64 71 78 83 103 108' 'BEGIN{split(pos,var)} {for (i in var) $0=gensub(/./,"|",var)} 1' test.txt | head I understood that the split function splits the pos string into... (2 Replies)
Discussion started by: mrcool4
2 Replies

2. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

3. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

4. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

7. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

8. 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

9. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

10. Shell Programming and Scripting

gawk and gensub

Hi, $ echo "Hellooo" | gawk '{print gensub(/o{3}/, "z", 1)}' doesn't return "Hellz" as expected while: $ echo "Hellooo" | awk '{print gensub(/o+/, "z", 1)}' produces "Hellz" correctly. Are the {m,n} quantifiers not supported in gensub? I know that sub or gsub could do the job. It's just an... (2 Replies)
Discussion started by: ripat
2 Replies
Login or Register to Ask a Question