简体   繁体   中英

Search array index with double quotes string using awk

File1:

"1"|data|er

"2"|text|rq

""|test2|req

"3"|test4|teq

File2:

1

2

3

Expected Output should be (file3.txt)

"1"|data|er

"2"|text|rq

"3"|test4|teq
awk -F''$Delimeter'' '{print $1}' file1.txt | awk '{gsub(/"/, "", $1); print $1}' | awk  'NF && !seen[$1]++' | sort -n > file2.txt

I am able to extract the ids 1,2,3 from file1 and removed the double quotes and written into the file2 but i need to search these 1,2,3 ids in my file1.txt("1","2","3"), problem is search not recognizing due to dobule qoutes in the file

awk 'BEGIN {FS=OFS="|"} NR==FNR{a[$1]; next} \"$1\" in a' file2.txt file1.txt > file3.txt

Could you please try following.

awk -v s1='"' '
FNR==NR{
  val=s1 $0 s1
  a[val]
  next
}
($1 in a)
' Input_file2 FS='|' Input_file1

Explanation: Adding detailed explanation for above code.

awk -v s1='"' '                        ##Starting awk program from here and creating variable s1 whose value is ".
FNR==NR{                               ##Checking condition FNR==NR which will be TRUE when first Input_file named Input_file2 is being read.
  val=s1 $0 s1                         ##Creating variable val whose value is s1 current line value and s1 here.
  a[val]                               ##Creating an array named a whose index is variable val.
  next                                 ##next will skip all further statements from here.
}                                      ##Closing FNR==NR BLOCK of this code here.
($1 in a)                              ##Checking condition if $1 of current line is present in array a then print that line of Input_file1.
' Input_file2 FS='|' Input_file1       ##Mentioning Input_file2 then setting FS as pipe and mentioning Input_file1 name here.

Let's say that your input is

"1"|data|er
"2"|text|rq
""|test2|req
"3"|test4|teq

And you want from these information 2 types of data :

  • The ids
  • The lines containing an id

The easiest way to achieve this is, I think, to first get lines that has id, then from this, retrieve the ids.

To do so :

$ awk -F'|' '$0 ~ /"[0-9]+"/' input1 >input3; cat input3
"1"|data|er
"2"|text|rq
"3"|test4|teq
$ sed 's/^"//; s/".*$//' input3 >input2; cat input2
1
2
3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM