LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-14-2008, 11:11 AM   #1
lmedland
LQ Newbie
 
Registered: Jun 2008
Location: England
Posts: 21

Rep: Reputation: 15
Question 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.
 
Old 08-14-2008, 12:47 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
I think ghostdog74 is the first LQ member you can search in the thread titles, now!
 
Old 08-14-2008, 06:34 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Bookmark & read this: http://www.grymoire.com/Unix/Awk.html
Come back if it still isn't clear.
 
Old 08-14-2008, 07:12 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by lmedland View Post

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)
 
Old 08-17-2008, 08:38 AM   #5
lmedland
LQ Newbie
 
Registered: Jun 2008
Location: England
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
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.
 
  


Reply

Tags
awk, gawk, manipulation, text


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk query jkeertir Linux - Newbie 3 01-17-2014 04:12 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
awk query jkeertir Linux - Networking 3 04-07-2008 08:07 AM
awk program. a little query! indiancosmonaut Programming 7 07-05-2007 09:48 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM


All times are GMT -5. The time now is 06:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration