Sponsored Content
Top Forums Shell Programming and Scripting How to grep a single field from a line? Post 302873443 by Akshay Hegde on Tuesday 12th of November 2013 06:44:03 AM
Old 11-12-2013
Try something like this

Code:
$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=1
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
eof     

1-sh
$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=2
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
eof

2-sh
$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=5
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
eof

5-sh

@RudyC I think you missed back slash

Code:
$ cat <<eof | awk -F"|" '/^aaa|bbb|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=1
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
aaa|1-sh
eof

1-sh
1-sh

$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=1
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
aaa|1-sh
eof

1-sh

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

replace single field out of 60

I have a pipe delimited file with over 60 fields on each line, and hundreds of lines...I want to replace field 6 only.... So what is the better way to do this command.... awk -F'|' '{print $1"|"$2"|"$3"|"$4"|"$5"| TXT6"$7"|"$8"|"....$59"|"$60}' infile > outfile I dont even know if this... (2 Replies)
Discussion started by: ajp7701
2 Replies

2. Shell Programming and Scripting

grep multiple words in a single line

Hi.. How to search for multiple words in a single line using grep?. Eg: Jack and Jill went up the hill Jack and Jill were best friends Humpty and Dumpty were good friends too ---------- I want to extract the 2nd statement(assuming there are several statements with... (11 Replies)
Discussion started by: anduzzi
11 Replies

3. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

4. Shell Programming and Scripting

awk - single quotes as field separator

How can I use single quotes as field separator in awk? (1 Reply)
Discussion started by: locoroco
1 Replies

5. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

6. Shell Programming and Scripting

sed to replace a field from a line with another field

i have something like this, cat filename.txt hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works i want to replace only 10.19.123.104 with different ip say 10.19.123.103 i tried this sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt ... (1 Reply)
Discussion started by: vivek d r
1 Replies

7. Shell Programming and Scripting

Two files comparision with single field

Hi , Im new to uxin environment and shell scripting.... please help me with the code for the following scenario..... file 1 contains the following fields abc 200 rupee IND cdf 400 dollar USA efg 300 euro GER hij 600 pound ENG file 2 SBI abc 321 dollar CANAD kvr mnd ... (6 Replies)
Discussion started by: shivaji_veer
6 Replies

8. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

9. Shell Programming and Scripting

Single Field to multiple fields searching

I have a fileA with one column (1000 rows), and fileB with 26 columns(13000 rows). I need to search each value of fileA with fileB and return all the 26 values from FileB to a new file- File C if matches. The search value (from FileA) may present in any of the 26 values in FileB. This value is not... (7 Replies)
Discussion started by: vamsikrishna928
7 Replies

10. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies
NUACLGEN(8)															       NUACLGEN(8)

NAME
nuaclgen - NUFW acl generator SYNOPSIS
nuaclgen [ --Schema eq|ineq ] [ --Aclname ACLDN ] [ --saddr NETWORK1 ] [ --daddr NETWORK2 ] [ --proto PROTONUMBER ] [ --sport P1[:P2] ] [ --dport P3[:P4] ] [ --OsName OSNAME ] [ --OsVersion OSVERSION ] [ --OsRelease OSRELEASE ] [ --AppName APPLICATION NAME ] [ --AppSig APPLI- CATION SIGNATURE ] [ --Separator SEPARATOR ] [ --jump [ACCEPT|DROP] ] [ --groups GROUPLIST ] nuaclgen [ --List ] [ --groups Id Group ] nuaclgen [ --Delete DN ] nuaclgen [ --help ] DESCRIPTION
This manual page documents the nuaclgen command. nuaclgen is an Access control list generator for the ldap backend of Nuauth, the authentication server of the NUFW package. Original packaging and informations and help can be found from http://www.nufw.org/ OPTIONS
--Schema eq|ineq Use equality schema or not (Equality schema requires dport be specified, and is the default. --AclName Acl DN Add an acl in the ldap tree with dn Acl DN --saddr Network Specify source network for the acl, with network of the form : aaa.bbb.ccc.ddd[/ee]. Default value : 0.0.0.0/0 --daddr Network Specify destination network for the acl, with network of the form : aaa.bbb.ccc.ddd[/ee]. Default value : 0.0.0.0/0 --proto Protocol number Specify protocol type for the acl --sport Source port Specify source port(s) for the Acl. NNNN:MMMM can be used to specify a port range. --dport Destination port Specify destination port(s) for the Acl. NNNN:MMMM can be used to specify a port range. --jump Decision Specify decision for the Acl. Has to be ACCEPT or DROP. --groups Groups list Specify the user group(s) on which Acl apply. Must be of the form : Group1[,Group2[,...]] --List --group Group ID List Acls for the Group ID group --Delete Dn Deletes the Acl stored in the provided Dn --help Displays a brief resume of available options and quits SEE ALSO
nufw(8) nuauth(8) AUTHOR
Nuauth was designed and coded by Eric Leblond, aka Regit (<eric@regit.org>) , and Vincent Deffontaines, aka gryzor (<vincent@gryzor.com>). Original idea in 2001, while working on NSM Ldap support. This manual page was written by Eric Leblond and copyrighted by INL (2003-2005) Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 2 as published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. 12 mars 2007 NUACLGEN(8)
All times are GMT -4. The time now is 09:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy