Sponsored Content
Full Discussion: awk search syntax
Top Forums Shell Programming and Scripting awk search syntax Post 303043218 by migurus on Tuesday 21st of January 2020 01:17:52 PM
Old 01-21-2020
awk search syntax

I need help with understanding awk search feature.


Here I am trying to search by 4th field in this file:


Code:
$ cat xx
471013,A20,asd,100,FEATURE UNKNOWN
171014,A22,sdf,101,FEATURE WITH SUB-ELEMENT
172822,20X,xcv,102,SUB-ELEMENT
101102,22X,asd,103,CODED

When I try the search feature I get nothing:
Code:
$ awk -F, '/$4 ~ "101"/{print}' xx        

$

When I just use IF statement it works as expected:
Code:
$ awk -F, '{ if($4 == 101)print; }' xx
171014,A22,sdf,101,FEATURE WITH SUB-ELEMENT
$

Please help me understand what do I do wrong in using the search // syntax
Thanks
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

AWK Syntax errors :/

I recently started as an intern and my manager wanted to see how well I would handle Korn Bourne shell scripting without any prior experience, I have prior programming experience but I keep running into syntax errors with AWK. Please take a look at my simple code and tell me what stupid mistake... (6 Replies)
Discussion started by: yongho
6 Replies

2. Shell Programming and Scripting

Help with Awk Syntax

I have written many awk commands which go in multiple lines. I have this confusion many times. Some time they work if i dont terminate them with "\" but some time error. Some time in "if" statements between if and else if i dont use ";" it gives error but sometimes it doesnt. The below... (4 Replies)
Discussion started by: pinnacle
4 Replies

3. Shell Programming and Scripting

awk syntax help

I don't get correct output when I run this command line: nmap -sP failedhost.com | grep -i failed | awk -F '{print $6}' I basically want it to return 'failedhost.com' but its just showing the output of the nmap scan. (8 Replies)
Discussion started by: streetfighter2
8 Replies

4. Shell Programming and Scripting

AWK syntax

Hi I am trying to understand AWK syntax so I tried this command which gives me the home directory of root awk 'BEGIN { FS = ":"} {if ($1 == "root") print $6 }' /etc/passwd I would know what are the following commands doing. The first one prints all /etc/passwd, second prints nothing. ... (4 Replies)
Discussion started by: wakatana
4 Replies

5. UNIX for Dummies Questions & Answers

awk syntax

Little bit confusing while using awk :confused::confused: In Sed while pattern search we can use "(double quotes) i mean $a=hello $cat file.txt |sed -n "/$a/p"this thing work fine But if i use it in awk it's not working How could i do the substitution of pattern by a variables and the... (1 Reply)
Discussion started by: posix
1 Replies

6. Shell Programming and Scripting

AWK syntax help

i have a ksh code that needs to be written in AWK. can someone please help me here? :( if }" | grep -c "$2") -gt 0 ] ; then print - "found $2 in array ignore" else print - "did not find $2 in array ignore" fi ignore=4ty56r ignore=er45ty . . . ignore=frhtg2 (27 Replies)
Discussion started by: usustarr
27 Replies

7. AIX

Help with syntax using AWK

I have a file which is comma separated and has quotes. I can use this command and awk -F"," '{ if ($4=="01" print $0 }' test.txt But this doesn't fetch me the data.since it has quotes. If the data has no quotes,the above command works fine. In Unix you can skip quote \" but this doesn't work.... (7 Replies)
Discussion started by: ganesnar
7 Replies

8. Shell Programming and Scripting

Syntax error using Awk

more report2.txt how to modify the AWK to print above out put for given n no of inputs using report2.txt file? out put should be (3 Replies)
Discussion started by: kanakaraju
3 Replies

9. Shell Programming and Scripting

awk syntax

Hi I have a bash file which will split a big file to many small files. But I got a syntax error.H="$(head -1 CCC.tped)" awk 'print $0 > $1 ".tped"' CCC.tped for f in $(ls *.tped); do echo "$H\n" "$(cat $f)" >$f; done And -bash-4.1$ bash split awk: print $0 > $1".tped" awk: ^ syntax error... (3 Replies)
Discussion started by: zhshqzyc
3 Replies

10. Shell Programming and Scripting

Help with the awk syntax

Hello Experts: While writing a script to help one of the posts on here, I end up writing a wrong one. I am very much eager to know how this can be corrected. Aim was to not print specified columns - lets say out of 100 fields, need to print all but 5th, 10th, 15th columns. Someone already... (13 Replies)
Discussion started by: juzz4fun
13 Replies
XML::Smart::FAQ(3pm)					User Contributed Perl Documentation				      XML::Smart::FAQ(3pm)

NAME
XML::Smart::FAQ - Frequently Asked Questions about XML::Smart. SYNOPSIS
This is the Frequently Asked Questions list for XML::Smart. QUESTIONS
For new questions send an e-mail to the author, but please, read first all the F.A.Q. Do I need to install XML::Parser to can use XML::Smart? No! XML::Smart already comes with 2 independent parsers, XML::Smart::Parser and XML::Smart::HTMLParser. If XML::Parser is found XML::Smart will use it by default, and the 2nd options will be XML::Smart::Parser. Note that for complex parsing XML::Parser is recommended, but XML::Smart::Parser will work fine too. What is the best version of XML::Smart to install? Is always the last! Always take a look for new versions before aks for help on XML::Smart. Note that internally XML::Smart is complex, since the main idea of it is to remove the complexity from the hand of the programmer. Actually the idea is to enable the Perl programmer to use and create XML data without really know the XML format. Where can I learn about XML? http://www.xml.com How to apply a DTD to a XML::Smart object tree? Take a look in the method apply_dtd(). Example of use: $xml->apply_dtd(q` <!DOCTYPE cds [ <!ELEMENT cds (album+)> <!ATTLIST cds creator CDATA date CDATA #REQUIRED > <!ELEMENT album (artist , tracks+)> <!ELEMENT artist (#PCDATA)> <!ELEMENT tracks (#PCDATA)> ]> `); This will format automatically elements, attributes, etc... How XML::Smart works? To create XML::Smart, first I have created the module Object::MultiType. With it you can have an object that works at the same time as a HASH, ARRAY, SCALAR, CODE & GLOB. So you can do things like this with the same object: $obj = Object::MultiType->new() ; $obj->{key} ; $obj->[0] ; $obj->method ; @l = @{$obj} ; %h = %{$obj} ; &$obj(args) ; print $obj "send data " ; Seems to be crazy, and can be more if you use tie() inside it, and this is what XML::Smart does. For XML::Smart, the access in the Hash and Array way paste through tie(). In other words, you have a tied HASH and tied ARRAY inside it. This tied Hash and Array work together, soo you can access a Hash key as the index 0 of an Array, or access an index 0 as the Hash key: %hash = ( key => ['a','b','c'] ) ; $hash->{key} ## return $hash{key}[0] $hash->{key}[0] ## return $hash{key}[0] $hash->{key}[1] ## return $hash{key}[1] ## Inverse: %hash = ( key => 'a' ) ; $hash->{key} ## return $hash{key} $hash->{key}[0] ## return $hash{key} $hash->{key}[1] ## return undef The best thing of this new resource is to avoid wrong access to the data and warnings when you try to access a Hash having an Array (and the inverse). Thing that generally make the script die(). Once having an easy access to the data, you can use the same resource to create data! For example: ## Previous data: <hosts> <server address="192.168.2.100" os="linux" type="conectiva" version="9.0"/> </hosts> ## Now you have {address} as a normal key with a string inside: $XML->{hosts}{server}{address} ## And to add a new address, the key {address} need to be an ARRAY ref! ## So, XML::Smart make the convertion: ;-P $XML->{hosts}{server}{address}[1] = '192.168.2.101' ; ## Adding to a list that you don't know the size: push(@{$XML->{hosts}{server}{address}} , '192.168.2.102') ; ## The data now: <hosts> <server os="linux" type="conectiva" version="9.0"/> <address>192.168.2.100</address> <address>192.168.2.101</address> <address>192.168.2.102</address> </server> </hosts> Than after changing your XML tree using the Hash and Array resources you just get the data remade (through the Hash tree inside the object): my $xmldata = $XML->data ; But note that XML::Smart always return an object! Even when you get a final key. So this actually returns another object, pointhing (inside it) to the key: $addr = $XML->{hosts}{server}{address}[0] ; ## Since $addr is an object you can TRY to access more data: $addr->{foo}{bar} ; ## This doens't make warnings! just return UNDEF. ## But you can use it like a normal SCALAR too: print "$addr " ; $addr .= ':80' ; ## After this $addr isn't an object any more, just a SCALAR! When I generate the XML data new lines ( ) are added to the content! You should use the options for the method data() and save() to not add identation to the generated data: $XML->data( noident => 1 ) ; ## or better: $XML->data( nospace => 1 ) ; Your question is not here? Just send me an e-mail. ;-P AUTHOR
Graciliano M. P. <gm@virtuasites.com.br> I will appreciate any type of feedback (include your opinions and/or suggestions). ;-P Enjoy and thanks for who are enjoying this tool and have sent e-mails! ;-P ePod This document was written in ePod (easy-POD), than converted to POD, and from here you know the way. perl v5.10.1 2004-12-08 XML::Smart::FAQ(3pm)
All times are GMT -4. The time now is 04:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy