awk search pattern in column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk search pattern in column
# 1  
Old 08-12-2014
awk search pattern in column

Want to search a pattern in column

using the below command which not helpful

Code:
awk -F"\|"  '$1 ==  '"${VAR}"'  {print $1,$2}' file

how to search using "==" with variable other than the below case.

Code:
awk -F"\|"  '$1 ~  /'"${VAR}"'/  {print $1,$2}' file

# 2  
Old 08-12-2014
Hello Roozo,

Could you please let us know the input and expected output for same please. It will really help us to iunderstand the requirement.


Thanks,
R. Singh
# 3  
Old 08-12-2014
file:
Code:
server1|121|base|1|
server2|232|case|1|
server3|454|base|1|

VAR=server1 ---> say we have server1 in the variable

When I tried with the below command I dint get any output.

Code:
awk -F"\|"  '$1 ==  '"${VAR}"'  {print $1,$2}' file

# 4  
Old 08-12-2014
Hi Roozo,

Could you please try following on same.

Code:
awk -vvar="server1" -F"|" '($1 == var) {print $0}' filename
server1|121|base|1|


Thanks,
R. Singh
# 5  
Old 08-12-2014

Thanks Ravinder,

I’m using this inside the shell, so I have modified like,
Code:
Method1:
awk  -vvar1=”${VAR}” –vvar2=”${SYSTEM}”   -F”\|”    ‘($1 == var1 ) && ($3 == var2) {print $1,$2}’   file 

Method2:
awk -F"\|"  '$1 ~  /'"${VAR}"'/  &&   $3  ~ /’”${SYSTEM}”’/   {print $1,$2}'   file



The above two methods are working fine in command line.

When I using this inside the ssh(remote), I’m getting the following error.

Method 1 error:
Code:
The error context is 
                                ( >>>  ==  <<<



Method 2 error:
Code:
The error context is 
                                ( >>>  ~  <<<



Code using:
Code:
ssh   user@box –P password  <<-EOF 
awk  -vvar1=”${VAR}” –vvar2=”${SYSTEM}”   -F”\|”    ‘($1 == var1 ) &&  ($3 == var2) {print $1,$2}’   file  | while read first second 
do 
echo “${first}”    “${second}” 
                if [ $? –eq 0 ]
                                then 
                                echo “true”
                else 
                                echo “false”
                fi
done  
EOF

---------- Post updated at 01:23 PM ---------- Previous update was at 12:48 PM ----------

Can any one help me on this please?
# 6  
Old 08-12-2014
Change the 1st line of your script to:
Code:
ssh   user@box -P password  <<-"EOF"

Without quotes here, the shell will perform variable expansions (among other things) on the text in the here-document.
# 7  
Old 08-12-2014
Thanks a lot Don.

The highlighted first and second variables return empty.

Code:
ssh   user@box –P password  <<-"EOF"
awk  -vvar1=”${VAR}” –vvar2=”${SYSTEM}”   -F”\|”    ‘($1 == var1 ) &&  ($3 == var2) {print $1,$2}’   file  | while read first second 
do 
echo “${first}”    “${second}” 
                if [ $? –eq 0 ]
                                then 
                                echo “true”
                else 
                                echo “false”
                fi
done  
EOF

Could you please let know how to resolve this?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find string based on pattern and search for its corresponding rows in column

Experts, Need your support for this awk script. we have only one input file, all these column 1 and column 2 are in same file and have to do lookup for values in one file(column1 and column2) but output we need in another file Need to grep row whose string contains 9K from column 1. When found... (6 Replies)
Discussion started by: as7951
6 Replies

2. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

3. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

4. Shell Programming and Scripting

Search Pattern and Print lines in Single Column

Hi Experts I have small query where I request the into a single file Suppose: File1: {Unique entries} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 Replies

5. Shell Programming and Scripting

awk with multiple pattern search

Want to fetch a column with multiple pattern using awk. How to achieve the same. Tried cat test address : 10.63.20.92/24 address : 10.64.22.93/24 address : 10.53.40.91/24 cat test | awk '{print $3}' |awk -F "/" '{print $1}' 10.63.20.92 10.64.22.93 10.53.40.91 Is there any... (2 Replies)
Discussion started by: Manasa Pradeep
2 Replies

6. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

7. UNIX for Dummies Questions & Answers

Pattern search using awk

Hi All, I am trying to find numbers with balance greater than 1 and less than equal 2 from the below file using awk Input file num ,bal 100199,1.708 100225,0 100226,0 100228,0.771166 100232,2 output file 100199,1.708 100232,2 I am using the following command for this... (2 Replies)
Discussion started by: pistachio
2 Replies

8. Shell Programming and Scripting

Pattern Search using AWK

Hi All, I have the below file data.txt.Using awk i want to grep all the zone data.Form the below command i can extact data upto of zone i give but i want it should print until next pattern. awk '/^Total Collection /{c=5;next}c-->0' zin45srs08.tools_utilization instead of c=5 is it possible... (5 Replies)
Discussion started by: ajaincv
5 Replies

9. Shell Programming and Scripting

awk search pattern

Hi, I have a log file which contains lines like below: 2010-07-19 07:13:19,021 ERROR system ...(text) 2010-07-19 07:22:03,427 ERROR system ...(text) class com... (text) 2010-07-19 07:23:19,026 ERROR system ...(text) class com... (text) each line is a separate line... I am given the a... (14 Replies)
Discussion started by: a27wang
14 Replies

10. UNIX for Dummies Questions & Answers

Search Pattern and add column

Hi, I have two files. file1 contents: aaa bbb ccc ddd eee fff ggg ddd www eee ggg dde qqq zzz hhh ddd file2 contents: mmm mmm mmm mmm Now I want to add file2 contents to end of lines in file1 where a line contains pattern "ddd" and it should look like this: file3 contents: aaa... (3 Replies)
Discussion started by: harjitsingh
3 Replies
Login or Register to Ask a Question