awk cut column based on string


 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support awk cut column based on string
# 8  
Old 02-14-2012
The standard awk is fairly weak. If you don't have access to GNU awk, install it. All the above solutions rely on GNU awk or nawk or at least Sun's xpg awk (which is an old version of nawk).
Code:
awk -v IGNORECASE=1 '{if( match($0,/-Tag:([^[:space:]]*)/,found)) print found[1]; }'

With nawk you might do something similar, but using sub() because nawk's match() isn't as cool as GNU's.
# 9  
Old 02-14-2012
Code:
awk -F"[ :-]" 'tolower($2)~/tag/{print "-"$2":"$3}' yourfile

or
Code:
awk '{split($1,a,":")}tolower(a[1])~/-tag/{print $1}' yourfile

or
Code:
awk '{NF=1;split($1,a,":")}tolower(a[1])~/-tag/' yourfile

Code:
$ cat tst
-tag:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical
-TAG:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical
-Tag:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical
-tAG:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical
$ awk -F"[ :-]" 'tolower($2)~/tag/{print "-"$2":"$3}' tst
-tag:messages
-TAG:messages
-Tag:messages
-tAG:messages
$ awk '{split($1,a,":")}tolower(a[1])~/-tag/{print $1}' tst
-tag:messages
-TAG:messages
-Tag:messages
-tAG:messages
$ awk '{NF=1;split($1,a,":")}tolower(a[1])~/-tag/' tst
-tag:messages
-TAG:messages
-Tag:messages
-tAG:messages


Last edited by ctsgnb; 02-14-2012 at 02:42 PM..
# 10  
Old 02-14-2012
Quote:
Originally Posted by otheus
[..]All the above solutions rely on GNU awk or nawk or at least Sun's xpg awk (which is an old version of nawk).
Are you certain about that otheus? I was under the impression that /usr/xpg4/bin/awk was introduced to Solaris later and does more to approach Posix standards than nawk on Solaris does, which stands for new awk, but that is only relative to ancient original awk...
# 11  
Old 02-14-2012
Not 100% sure, but I know Kernighan was maintaining nawk at least through 2007, and the open BSD project has been maintaining it since, and Solaris, well, I think they brought awk over from System V back in the 90s or maybe even before then with SunOS 4.x

---------- Post updated at 03:54 PM ---------- Previous update was at 03:16 PM ----------

Quote:
Originally Posted by Scrutinizer
Are you certain about that otheus? I was under the impression that /usr/xpg4/bin/awk was introduced to Solaris later and does more to approach Posix standards than nawk on Solaris does, which stands for new awk, but that is only relative to ancient original awk...
Follow-up:
From the FIXES file in awk.zip downloaded from Kernighan's web page:
Code:
Jun 1, 2003:
	subtle change to split: if source is empty, number of elems
	is always 0 and the array is not set.

From Solaris 10 (2005) xpg-awk:
Code:
$ /usr/xpg4/bin/awk 'BEGIN { print split(null,out,FS) }' </dev/null
0

So it would seem Solaris DID keep nawk up-to-date w.r.t Kernighan's version.

Then again....
Code:
Jan 1, 2002:
	length(arrayname) returns number of elements; thanks to 
	arnold robbins for suggestion

And on Sun's implementation:
Code:
$ /usr/xpg4/bin/awk 'BEGIN { split("test",out,/es/); print out[1]; print length(out)}' </dev/null
t
0


Last edited by otheus; 02-14-2012 at 11:01 AM.. Reason: oops! LOL copied wrong output.
# 12  
Old 02-14-2012
@otheus, Interesting, I think though you should be comparing these Solaris nawk, not /usr/xpg4/bin/awk, which should not be following Kernighan's changes, but rather strive to be Posix compliant, no? What is the output of the same commands with nawk ?
# 13  
Old 02-14-2012
First, I think you should split this thread into the Underground forum, for instance, and link to it Smilie

Second, Kerhnighan *is* the author of nawk. What Solaris did to what they call nawk is anyone's guess.

Third, Solaris lists the nawk man page and xpg4/awk man page as the same entity (yet oddly, the files differ vastly in size).

Fourth, nawk explicitly errors with length(arrayname):
Code:
$ nawk 'BEGIN { split("test",out,/es/); print out[1]; print length(out)}' </dev/null
t
nawk: can't read value of out; it's an array name.
 source line number 1

# 14  
Old 02-14-2012
I think you are right, let's do that if you think it is interesting (I do), but what shall we call the thread? /usr/xpg4/bin/awk vs. nawk on Solaris? I thought in post#8 you meant on Solaris /usr/xpg4/bin/awk is an old version of nawk , i.e. the current version on Solaris. And my point was/is that nawk on Solaris is not as compliant as /usr/xpg4/bin/awk and therefore the latter is preferable to nawk on Solaris.

But on rereading you seem to be referring to a recent version of nawk on different systems. But in many other systems nawk is either non-existing or a link to gawk or mawk and on yet others awk is nawk (or bwk).

Yes, Kernighan is the author of nawk, but length() operating on an array is an added feature and is not part of the Posix specification (and unnecessary).
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

Awk/sed/cut to filter out records from a file based on criteria

I have two files and would need to filter out records based on certain criteria, these column are of variable lengths, but the lengths are uniform throughout all the records of the file. I have shown a sample of three records below. Line 1-9 is the item number "0227546_1" in the case of the first... (15 Replies)
Discussion started by: MIA651
15 Replies

3. UNIX for Dummies Questions & Answers

Count occurrence of string (based on type) in a column using awk

Hello, I have a table that looks like what is shown below: AA BB CC XY PQ RS AA BB CC XY RS I would like the total counts depending on the set they belong to: if search pattern is in {AA, BB, CC} --> count them as Type1 | wc -l (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

4. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

5. Shell Programming and Scripting

To cut a string based on last index of identifier

here below is sample string null pointer dereference of 'resourceList' where null is returned from a method/opt/bld/fetch/ds/interzone/notification/LocalLineStatusNotificationListener.java:79 null pointer dereference of 'reList' where null is returned from a... (3 Replies)
Discussion started by: vivek d r
3 Replies

6. Shell Programming and Scripting

Pick the column value based on another column using awk or CUT

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Please someone help me to resolve this issue. Source column1 column2 column3 column4... (2 Replies)
Discussion started by: Ganesh L
2 Replies

7. UNIX for Dummies Questions & Answers

How to cut from a text file based on value of a specific column?

Hi, I have a tab delimited text file from which I want to cut out specific columns. If the second column equals one, I want to cut out columns 1 and 5 and 6. If the second column equals two, I want to cut out columns 1 and 5 and 7. How do I go about doing that? Thanks! (4 Replies)
Discussion started by: evelibertine
4 Replies

8. UNIX for Dummies Questions & Answers

how to cut based on a string

Hi I have some data where each line will look something like this: Time, name, i.d number, RB: 0.9949; RMQA: 0.0005; RB: 0.9951; RRA: 0.3; RA: 0.995; RA: 0.996; EA: 0.99105 etc. I want to cut out all the RB: and RA:'s with the numbers after. so in the above example i'd be left... (3 Replies)
Discussion started by: gvc
3 Replies

9. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

10. UNIX for Dummies Questions & Answers

Cut from tables based on column values

Hello, I have a tab-delimited table that may contain 11,12 or 13 columns. Depending on the number of columns, I want to cut and get a sub table as shown below. However, the awk commands in the code seem to be an issue. What should I be doing differently? #cut columns 1-2,4-5,11 when 12 &... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies
Login or Register to Ask a Question