Grep correct pattern with special character and variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep correct pattern with special character and variables
# 1  
Old 08-21-2013
Grep correct pattern with special character and variables

Code:
cat file
time="north_south_east_west_08:00" location="A" start="left" status="ok" end="north" 
time="north_south_east_west_12:00" location="C" start="right" status="ok" end="south"
time="north_south_east_west_23:00" location="G" start="left" status="ok" end="east"
time="north_south_east_west_10:40" location="K" start="top" status="ok" end="south"
time="north_south_east_west_08:50" location="P" start="top" status="x_ok" end="west"
time="north_south_east_west_09:40" location="D" start="down" status="ok" end="north"
time="north_south_east_west_11:23" location="J" start="right" status="x_ok" end="west"
time="north_south_east_west_07:45" location="E" start="left" status="ok" end="west"
time="north_south_east_west_18:10" location="E" start="down" status="ok" end="east"
time="north_south_east_west_13:55" location="O" start="left" status="x_ok" end="south"

i want the output to be printed out only coloum 1,3,5 and only either end=north/south
set var1="north"
set var2="south"

how can i integrate variable into my grep, i kind of messup with the " ' here
awk '{print $1,$3,$5}' file | grep 'end="$var1"\|end="$var2"' | less -S

expected output to be
time="north_south_east_west_08:00" start="left" end="north" 
time="north_south_east_west_12:00" start="right" end="south"
time="north_south_east_west_10:40" start="top" end="south"
time="north_south_east_west_09:40" start="down" end="north"
time="north_south_east_west_13:55" start="left" end="south"


Last edited by ctphua; 08-21-2013 at 05:38 AM..
# 2  
Old 08-21-2013
can you provide and example of the output your after? It may be better for me to provide a solution that way?
# 3  
Old 08-21-2013
Code:
awk '/north/||/south/ {print $1,$3,$5}' file

# 4  
Old 08-21-2013
Please use code tags as required by forum rules!

Try
Code:
awk '/south"$|north"$/ {print $1,$3,$5}' file
time="08:00" start="left" end="north"
time="12:00" start="right" end="south"
time="10:40" start="top" end="south"
time="09:40" start="down" end="north"
time="13:55" start="left" end="south"

# 5  
Old 08-21-2013
Including variables:

Code:
awk -v v1=$var1 -v v2=$var2 -F'"' '{if ($(NF-1)==v1||$(NF-1)==v2)  {FS=" ";print $1,$3,$5;FS="\""}}' file


Last edited by krishmaths; 08-21-2013 at 05:47 AM..
# 6  
Old 08-21-2013
Quote:
Originally Posted by RudiC
Please use code tags as required by forum rules!

Try
Code:
awk '/south"$|north"$/ {print $1,$3,$5}' file
time="08:00" start="left" end="north"
time="12:00" start="right" end="south"
time="10:40" start="top" end="south"
time="09:40" start="down" end="north"
time="13:55" start="left" end="south"

Can you walk us through what awk is doing here?
Curious. Smilie
# 7  
Old 08-21-2013
Quote:
Originally Posted by briandanielz
can you provide and example of the output your after? It may be better for me to provide a solution that way?
updated in first thread. thanks

Quote:
Originally Posted by krishmaths
Code:
awk '/north/||/south/ {print $1,$3,$5}' file

erm.. i miss up some string in the first post.. i have modified my file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep -F for special character

a='CASH$$A' /usr/xpg4/bin/grep -F "$a" *.txt It is not able to grep CASH$$A string as it contains special character $$. I also tried with /usr/xpg4/bin/grep -F '$a' *.txt but still not working. I have to assign CASH$$A to a variable and serach that variable..i dont want to search the... (8 Replies)
Discussion started by: millan
8 Replies

2. Shell Programming and Scripting

Merging two special character separated files based on pattern matching

Hi. I have 2 files of below format. File1 AA~1~STEVE~3.1~4.1~5.1 AA~2~DANIEL~3.2~4.2~5.2 BB~3~STEVE~3.3~4.3~5.3 BB~4~TIM~3.4~4.4~5.4 File 2 AA~STEVE~AA STEVE WORKS at AUTO COMPANY AA~DANIEL~AA DANIEL IS A ELECTRICIAN BB~STEVE~BB STEVE IS A COOK I want to match 1st and 3rd... (2 Replies)
Discussion started by: crypto87
2 Replies

3. Linux

Creating 2 variables from a multiple pattern grep

first time poster here Im pretty new to grep and linux in general and I spent pretty much all day yesterday researching and coming up with a grep command to help with my university project. I am attempting to create a proof of concept bash script to scan the network using ngrep, find appropriate... (7 Replies)
Discussion started by: scottish_jason
7 Replies

4. Shell Programming and Scripting

Grep special pattern in 3rd column

I have a file (test.dat) that has this pattern: 1000 000001 (92.431343802235503, 90.0) 1000 000002 (87.568656197764497, 80.0) 1000 000003 (150.75815307316083, 150.0) 1000 000004 (29.241846926839159, 20.0) 1000 000005 (110.02128542766, 110.0) 1000 000006 (69.978714572339996, 60.0) 1000... (8 Replies)
Discussion started by: kayak
8 Replies

5. Shell Programming and Scripting

SED equivalent for grep -w -f with pattern having special characters

I'm looking for SED equivalent for grep -w -f. All I want is to search a list of patterns from a file. Also If the pattern doesn't match I do not want "null returned", rather I would prefer some text as place holder say "BLANK LINE" as I intend to process the output file based on line number. ... (1 Reply)
Discussion started by: novice_man
1 Replies

6. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

7. Linux

how to grep special character regular expression?

Hi :) I have 2 files file1: SNP_A-2208459 SNP_A-4215188 SNP_A-2012248 SNP_A-1882998 file2: CHR SNP UNADJ BONF HOLM * * * etc. 19 SNP_A-2236481 1.742e-26 5.442e-21 13 SNP_A-4204405 8.643e-07 1.505e-06 3 SNP_A-1860908... (11 Replies)
Discussion started by: sogi
11 Replies

8. Shell Programming and Scripting

pattern matching on any special character in Unix

Hi, I have field in a file which would come with any special character, how do i check that field? Eg: @123TYtaasa>>>/ 131dfetr_~2 In the above example, how do I add pattern for any special character on the keyboard. Thanks (3 Replies)
Discussion started by: techmoris
3 Replies

9. UNIX for Advanced & Expert Users

grep in special character

All, I am trying to grep "-----" from a test when i use this i am getting the below error. What is the reason for this ?????... How can i over come this ##) echo "----------------- test_sys_job -----------------" | grep "-----------------" grep: illegal option -- - grep: illegal... (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

10. UNIX for Advanced & Expert Users

Escaping special character stored in variables : perl

Hi just for regular use i m working on small module written in perl for getting date in specified format like i have to specify date format and then seperator to seperate date i am 95% done. now i m sure explanation i gave is not good enough so i am putting output here : C:\Documents and... (2 Replies)
Discussion started by: zedex
2 Replies
Login or Register to Ask a Question