vishal
vishal

Reputation: 43

double quotes are coming in the file

I am facing a problem using awk. Actually I have two types of files. Below is the contain of file.

File1

x|y|z|a|b|c
x|y|z|a|b|c
x|y|z|a|b|c

File2

"x"|"y"|"z"|"a"|"b"|"c"
"x"|"y"|"z"|"a"|"b"|"c"
"x"|"y"|"z"|"a"|"b"|"c"

I have to search some value from second column by using awk. but the problem is that file1 has records without quotes whereas file2 has records with double quotes. I need to create a single logic to search the value from the second column.

Can anyone help?

Upvotes: 0

Views: 59

Answers (3)

Ed Morton
Ed Morton

Reputation: 203254

I wish you had posted some sample input (not just the format with x/y placeholders but actual data) and expected output so we could test our solutions but this SHOULD do what you want:

awk -F'"?[|]"?' '$2 == whatever' file

Replace "whatever" with whatever you're looking for, and replace == with ~ if you want an RE comparison rather than exact comparison.

If you had to be able to look in the first and last fields too, then there's various solutions and the "best" one would be determined by your real data but here's one approach if you have no undesirable RE metacharacters in "whatever":

awk -F'"?[|]"?' ' $1 ~ "^\"?" whatever    "$"' file
awk -F'"?[|]"?' '$NF ~ "^"    whatever "\"?$"' file

Upvotes: 1

fedorqui
fedorqui

Reputation: 289535

I noticed there is no need to clean the file. To look for value y in both files you can do the following:

$ awk -F"|" '$2~/y/' file1 file2
x|y|z|a|b|c
x|y|z|a|b|c
x|y|z|a|b|c
"x"|"y"|"z"|"a"|"b"|"c"
"x"|"y"|"z"|"a"|"b"|"c"
"x"|"y"|"z"|"a"|"b"|"c"

With a more complicated input:

$ cat file1
x|y|z|a|b|c
x|T|z|a|b|c
x|aa|y|a|b|c
$ cat file2
"x"|"y"|"z"|"a"|"b"|"c"
"x"|"22"|"z"|"a"|"b"|"c"
"x"|"t"|"y"|"a"|"b"|"c"

We get:

$ awk -F"|" '$2~/y/' file1 file2
x|y|z|a|b|c
"x"|"y"|"z"|"a"|"b"|"c"

Upvotes: 1

vikingsteve
vikingsteve

Reputation: 40378

Just filter the quotes out with sed 's/\"//g' and pipe them to awk with |

$ sed 's/\"//g' file2
x|y|z|a|b|c
x|y|z|a|b|c
x|y|z|a|b|c

Your end result would be something like

$ sed 's/\"//g' file2 | awk <magic goes here...>

Upvotes: 0

Related Questions