Find the answer to your Linux question:
Results 1 to 2 of 2
Hi everyone, Please some help, with my question (I´m using Cygwin). How can I join the 2 AWK scripts showed below in a unique script avoiding use temp file? I ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    6

    Join 2 AWK scripts problem

    Hi everyone,

    Please some help, with my question (I´m using Cygwin).

    How can I join the 2 AWK scripts showed below in a unique script avoiding use temp file?

    I have this input file Pipe separated.
    Code:
    CATEGORY1|CATEGORY2|CATEGORY3|CATEGORY4
    String3|MUSIC/OLDIES/POP/80S/SONGW|String4|
    String4|MUSIC/OLDIES/ROCK/70S/SONGX|String5|
    String2|MUSIC/OLDIES/TECHNO/80S-90S/SongY|String7|
    String1|MUSIC/OLDIES/BLUES/80S/SONGZ|String2|
    String5|MUSIC/ALTERNATIVE/DANCE/90S/SONGXY|String1|
    And I´ve been building a script adding individual AWK routines to do the tasks explained below:

    Code:
    awk 'BEGIN {OFS=FS="|"; IGNORECASE=1} # Set FS=| and case insensitive
    NR>1{$4=$2}           ### Making Colum 4 = Column 2, leaving original headers 
    NR==1{$4="NEW CATEGORY"}             ### Renaming col $4 header (1st line)
    
    ### Remove 2 last sub folder levels in col4 
    {$4 = gensub(/([^/]*\/[^/]*\/).*$/,"\\1",1,$4); sub(/music\//,"",$4)}
    #{sub(/\/.*/,"",$4);sub(/music\//,"",$4)}
    
    ### 1st Filter to search for these patterns
    /CATEGORY/||/string1/||/string3/ {
    
    ### 2do filtro to exclude some other patterns in column 4 ###
    if ($4 !~ /string2|string4/) 
    
    ### Imprimiendo columnas en nuevo orden ###
    print $1,$4,$3,$2}' infile > infile_Temp
    
    ### To capitalize Column 2
    awk -v col=2 'BEGIN{OFS=FS="|"} NR>1{n=split(tolower($col),a," "); $col=toupper(substr(a[1],1,1)) substr(a[1],2)
    for(i=2;i<=n;i++) {$col=$col " " toupper(substr(a[i],1,1)) substr(a[i],2)}}1' infile_Temp > outfile
    
    rm infile_Temp # To remove temp file
    Well, the output I get with this input file and this script is:
    Code:
    CATEGORY1|New category|CATEGORY3|CATEGORY2
    String3|Oldies/|String4|MUSIC/OLDIES/POP/80S/SONGW
    String1|Oldies/|String2|MUSIC/OLDIES/BLUES/80S/SONGZ
    String5|Alternative/|String1|MUSIC/ALTERNATIVE/DANCE/90S/SONGXY
    As a mentioned in my question above, I want to merged these 2 scripts in a unique one, adding at the end the
    routine that "Capitalizes Column 2", and if I achieve this I´ll be able to do a processing without using Temp files.

    Maybe somebody could help with this, because I´ve been trying without success so far.

    Thanks in advance.

  2. #2
    Just Joined!
    Join Date
    May 2010
    Posts
    6
    Guys, after viewing the script and some tests I could change in the right way,

    Only moved the "capitalize script" before the 1rst filter and I don´t have temp files anymore
    Code:
    awk -v col=2 'BEGIN {OFS=FS="|"; IGNORECASE=1} # Set FS=| and case insensitive
    NR>1{$4=$2}           ### Making Colum 4 = Column 2, leaving original headers 
    NR==1{$4="NEW CATEGORY"}             ### Renaming col $4 header (1st line)
    
    ### Remove 2 last sub folder levels in col4 
    {$4 = gensub(/([^/]*\/[^/]*\/).*$/,"\\1",1,$4); sub(/music\//,"",$4)}
    #{sub(/\/.*/,"",$4);sub(/music\//,"",$4)}
    
    ### To capitalize Column 2
    NR>1{n=split(tolower($col),a," "); $col=toupper(substr(a[1],1,1)) substr(a[1],2)
    for(i=2;i<=n;i++) {$col=$col " " toupper(substr(a[i],1,1)) substr(a[i],2)}}
    
    ### 1st Filter to search for these patterns
    /CATEGORY/||/string1/||/string3/ {
    
    ### 2nd filter to exclude some other patterns in column 4 ###
    if ($4 !~ /string2|string4/) 
    
    ### Printing column in new order ###
    print $1,$4,$3,$2}' infile > output
    Thanks anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...