LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Ghostdog74 | AWK Query (https://www.linuxquestions.org/questions/programming-9/ghostdog74-%7C-awk-query-662781/)

lmedland 08-14-2008 11:11 AM

Ghostdog74 | AWK Query
 
Hi,

You posted on a query I had over a data manipulation task. The code you wrote seems well structured and I would like to understand it a little more.

Code:

awk 'BEGIN{OFS=FS=", "
 c["N"]="01"
 c["C"]="02"
 c["L"]="03"
}
NR==1{print}
NR>1{
 print $1,$2, c[substr($3,1,1)] substr($3,2) "01"
}
' file

From what I understand so far, the BEGIN OFS skips the first line. I can see that variable (?) "c" is detailing the first two numbers to use. How does this work?

Then NR displays the first line? Then I get stuck on the substr syntax.

If you could explain a little more it would be helpful. I need to amend the script so that in an event that a code's 3 characters (after the first character) are the same as another codes three characters, the 01 at the end will increment by 1.

Example:-

CBUPP = 02BUP01
CBUPI = 02BUP02
CBUPK = 02BUP03


At the moment the script will obviously produce the following:-

02BUP01
02BUP01
02BUP01


Thanks for the help.

colucix 08-14-2008 12:47 PM

I think ghostdog74 is the first LQ member you can search in the thread titles, now! ;)

chrism01 08-14-2008 06:34 PM

Bookmark & read this: http://www.grymoire.com/Unix/Awk.html
Come back if it still isn't clear.

ghostdog74 08-14-2008 07:12 PM

Quote:

Originally Posted by lmedland (Post 3247525)

From what I understand so far, the BEGIN OFS skips the first line. I can see that variable (?) "c" is detailing the first two numbers to use. How does this work?

the BEGIN{} block is a place where you initialize variables. ie, before awk process the files. "c" is called associative arrays. They are equivalent to this piece of code snippet
Code:

if ( var=="N" ) {
 c="01
}else if ( var=="C" ) {
 c="02"
}

Quote:

Then NR displays the first line? Then I get stuck on the substr syntax.
NR means record number. The first line , NR == 1. Second record, NR==2 and so on. As for substr, you can read about it in the manual (or the one chris provided)

lmedland 08-17-2008 08:38 AM

Quote:

Originally Posted by ghostdog74 (Post 3247987)
the BEGIN{} block is a place where you initialize variables. ie, before awk process the files. "c" is called associative arrays. They are equivalent to this piece of code snippet
Code:

if ( var=="N" ) {
 c="01
}else if ( var=="C" ) {
 c="02"
}


NR means record number. The first line , NR == 1. Second record, NR==2 and so on. As for substr, you can read about it in the manual (or the one chris provided)

Thanks for the info everyone. :)


All times are GMT -5. The time now is 10:28 AM.