Top Forums Shell Programming and Scripting Displaying the first field if the second field matches the pattern using Perl Post 302727119 by royalibrahim on Monday 5th of November 2012 09:11:18 PM
Quote:
Originally Posted by elixir_sinari
The code you've provided does what it's supposed to do.

First of all, you are opening the file myfile, reading it completely and then closing the associated filehandle, once for each line in the file dummy_file.

For every line from dummy_file in which the 2nd field $F[1] (split on white-space using the -a run-time switch) matches the pattern patt anchored at the end, the first element of the list slice (split(" ", $i))[0], obtained by splitting the line from myfile on white-space, is displayed.

What exactly are you trying to do? Please elaborate with input and output samples.
Thank you, but the dummy file is just an empty file to fulfill or complete the syntax (else Perl reads from stdin). The actual file in action is "myfile" where I am looping over its contents by reading from the file handle 'F'. The white space splitting is done on the myfile's contents and not of the dummy file.

Lets say, my input is, myfile's contents:

aa bb cc
1a 2a 3a

I would like to get the output as:
aa

where 'bb' is the pattern to be matched.
 
Test Your Knowledge in Computers #812
Difficulty: Medium
In CSS, the rgba() function define colors using the red-green-blue-accent (RGBA) model.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print line if first Field matches a pattern

Hi All, I would like my code to be able to print out the whole line if 1st field has a dot in the number. Sample input and expected output given below. My AWK code is below but it can;t work, can any expert help me ? Thanks in advance. {if ($1 ~ /*\.*/) { print $0 }} Input: ... (2 Replies)
Discussion started by: Raynon
2 Replies

2. Shell Programming and Scripting

Displaying lines of a file where the second field matches a pattern

Howdy. I know this is most likely possible using sed or awk or grep, most likely a combination of them together, but how would one go about running a grep like command on a file where you only try to match your pattern to the second field in a line, space delimited? Example: You are... (3 Replies)
Discussion started by: LordJezoX
3 Replies

3. Shell Programming and Scripting

adding field values if field matches

hi i have file as below , i want to add duplicate records like bell_bb to one record with valuve as 15 ( addition of both ) any oneline awk script to achive this ? header 0 CAMPAIGN_NAME 1 Bell_BB 14 Bell_MONTHLY 803 SOLO_UNBEATABLE 644 Bell_BB 1 Bell_MONTHLY 25 SOLO_UNBEATABLE... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies

4. Shell Programming and Scripting

How to print line if field matches?

Hi all, I got several lines line this a b c d e 1 e a 1 c d e 3 f a b c 1 e 8 h a b c d e 1 w a 1 c d e 2 w a b c d e 1 t a b c d e 7 4 How can I print the line if 1 is the field one before the last field? Basicly this 2 field ? a b c d e 1 e a b c d e 1 t The file I got is... (7 Replies)
Discussion started by: stinkefisch
7 Replies

5. Shell Programming and Scripting

awk to sum specific field when pattern matches

Trying to sum field #6 when field #2 matches string as follows: Input data: 2010-09-18-20.24.44.206117 UOWEXEC db2bp DB2XYZ hostname 1 2010-09-18-20.24.44.206117 UOWWAIT db2bp DB2XYZ hostname ... (3 Replies)
Discussion started by: ux4me
3 Replies

6. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

7. Shell Programming and Scripting

Displaying a field completely

Version: AIX 6.1 (korn shell) In the below output, the field with the heading 'Address' has some names like hwproc214-priv1.gnas.wrd.netwhich are only partially displayed. $ netstat -i Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll en2 1500 link#2 ... (3 Replies)
Discussion started by: polavan
3 Replies

8. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

9. UNIX for Dummies Questions & Answers

Displaying field of NR, not the line #

Within AWK, how do you display a field of NR? Here's my code: awk '(NR>1) && (P1=$1-w)>=100000 {print "increase of" " " P1*.0000179," " "kW at" " " 'NR*60/431900' " " "minutes" "\n" "change from" " " 'NR-10($1)' " " "kW to" " " 'NR+70($1)' "\n"}{w=$1}' filename I can change NR and print... (3 Replies)
Discussion started by: markymarkg123
3 Replies

10. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies
HXCITE(1)							  HTML-XML-utils							 HXCITE(1)

NAME
hxcite - replace bibliographic references by hyperlinks SYNOPSIS
hxcite [ -b base ] [ -p pattern ] [ -a auxfile ] [ -m marker ] [ -c ] bibfile [ file ] DESCRIPTION
The hxcite commands copies the file to standard output, looking for strings of the form [[label]]. The label may not include white space and the double pair of square brackets must enclose the label without any spaces in between. If hxcite finds the label in the bibfile, the string is replaced by the pattern. The pattern can include certain variables. If the label is not found in bibfile, it is left unchanged. The default pattern replaces the string with a hyperlink, but if the -p option is used, the replacement can be any pattern. The input doesn't even have to be HTML. If the label is enclosed in {{...}} instead of [[...]], it is copied to the output unchanged and not replaced by the pattern, but the label is still searched in the bibfile. OPTIONS
The following options are supported: -p pattern Specifies the pattern by which the string [[label]] is replaced. The pattern may include the variables %b (which is replaced by the value of the -b option), %m (which is replaced by the value of the -m option) and %L (which is replaced by the label). The default pattern is <a href="%b#%L" rel="biblioentry">[%L]<!--{{%m%L}}--></a> -b base Sets the value for the %b variable in the pattern. Typically this is set to a relative or absolute URL. By default this value is an empty string. -a auxfile All labels that have been found and replaced are also written to a file. This is so that hxmkbib(1) can find them and create a bibliography. The default auxfile is constructed from the name of the file by removing the last extension (if any) and replacing it by ".aux". If no file is given, the default name is "aux.aux". -m marker By default, the program looks for "[[name]]", but it can be made to look for "[[#name]]" where # is some string, usually a symbol such as '!' or '='. This allows references to be classified, e.g., "[[!name]]" for normative references and "[[name]]" for non- normative references. -c Causes "[[name]]" to be ignored when it occurs inside XML comments ("<!--...-->"). This is useful for files where such labels occur in comments, to avoid that they be expanded and possibly lead to invalid output; useful also if hxcite is used for non-HTML files which may contain "<!--" that are not comment. Occurrences of "{{name}}" are not affected by -c. (But see warning under BUGS below.) OPERANDS
The following operands are supported: bibfile The name of a bibliographic database must be given. It must be a file in refer(1) format and every entry must have at least a %L field, which is used as label. (Entries without such a field will be ignored.) file The name of the input file is optional. If absent, hxcite will read from stdin. The file does not have to be an HTML file, but the default pattern (see the -p option) assumes HTML. EXIT STATUS
The following exit values are returned: 0 Successful completion. > 0 An error occurred. Usually this is because a file could not be opened. Very rarely it may also be an out of memory error. VERSIONS
March 2000: created by Bert Bos <bert@w3.org> as "cite". August 2008: renamed to "hxcite". Currently maintained by Bert Bos. BUGS
hxcite does not actually parse HTML or XML input and the -c option simply treats every occurence of "<!--" as the start of an XML comment, even if it occurs inside an attribute value or a CDATA section. Likewise for "-->" for the end of a comment. There is currently no way to use numbers for references (e.g., "[1]", "[2]") instead of the labels ("[Lie1996]", "[UTN22]"). hxcite requires the %L (label) field to be present in every entry in bibfile, which is not the case for refer(1). hxcite does not imple- ment refer's keyword search. EXAMPLE
The following looks for reference of the form "[[!label]]" in "myfile.html", skipping references that occur inside HTML comments, and looks up the labels in "biblio.ref". The output is written to "new.html" and the list of recognized labels to "myfile.aux". hxcite -c -m '!' biblio.ref myfile.html > new.html SEE ALSO
asc2xml(1), refer(1), hxmkbib(1), hxnormalize(1), hxnum(1), hxprune(1), hxtoc(1), hxunent(1), xml2asc(1), UTF-8 (RFC 2279) 6.x 10 Jul 2011 HXCITE(1)

Featured Tech Videos

All times are GMT -4. The time now is 11:53 PM.
Unix & Linux Forums Content Copyright 1993-2019. All Rights Reserved.
Privacy Policy