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
# 1  
Old 02-13-2012
awk cut column based on string

Using awk I required to cut out column contain word "-Tag" regardles of any order of contents and case INsensitive

-Tag:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical

Please Guide ......


--Shirish Shukla

---------- Post updated at 05:58 AM ---------- Previous update was at 05:50 AM ----------

Have came with this but it's case sensitive

Code:
# echo "-tag:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical" | awk '{for(i=1;i<=NF;i++) if($i ~ /tag/) print $i}'
-tag:messages



Smilie

Last edited by Shirishlnx; 02-13-2012 at 06:56 AM..
# 2  
Old 02-13-2012
Code:
 
$ cat test.txt
-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
$ nawk '{for(i=1;i<=NF;i++){if($i~/[Tt][Aa][Gg]/)print $i}}' test.txt
-tag:messages
-TAG:messages
-Tag:messages
-tAG:messages

# 3  
Old 02-13-2012
Code:
grep -io "\-tag:messages" inputfile

# 4  
Old 02-13-2012
@itamaraj

Sorry nawk not installed have to achieve via awk only ...
Thanks..

---------- Post updated at 06:19 AM ---------- Previous update was at 06:16 AM ----------

@balajesuri

Thanks am aware it ... but i want to achieve this via awk only ...

--Shirish
# 5  
Old 02-13-2012
use awk instead of nawk
# 6  
Old 02-13-2012
Thanks All !!!

Here what had used, IGNORECASE=1 with awk

Code:
[root@nagios Shirish@Shukla]# echo "-Tag:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical" |  \ 
> awk 'IGNORECASE=1 {for(i=1;i<=NF;i++) if($i ~ /tAg/) print $i}'
-Tag:messages
[root@nagios Shirish@Shukla]# echo "-Tag:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical" | \
> awk 'IGNORECASE=1 {for(i=1;i<=NF;i++) if($i ~ /tag/) print $i}'
-Tag:messages
[root@nagios Shirish@Shukla]# echo "-Tag:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical" | \
> awk 'IGNORECASE=1 {for(i=1;i<=NF;i++) if($i ~ /taG/) print $i}'
-Tag:messages
[root@nagios Shirish@Shukla]#

# 7  
Old 02-14-2012
whenever you post your question, post your OS and shell details.

that is easy for giving suggestions and ideas

---------- Post updated at 12:59 PM ---------- Previous update was at 12:58 PM ----------

As IGNORECASE only works in gnu awk.
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