I have a data.csv
file, its content:
0,H,"First Job"
0,M,"Second Job"
0,L,"Third Job"
...
in which the user can search its term on the 3rd column like below:
./find.sh Third
#!/bin/bash
awk -F"," -v findit="$1" '$3 ~ findit' data.csv
I want to change the displaying format of the result of awk
to show the line of result, like this:
3 | 0 | L | "Third Job"
number 3 indicates that the result were found on the 3rd line. How can I do this?
CodePudding user response:
With your shown samples, please try following awk
code.
awk -v findit="$1" 'BEGIN{FS=",";OFS=" | "} $3~findit{$1=$1;print FNR,$0}' Input_file
Explanation: simple explanation would be, creating awk
variable which has value of shell variable $1
in it and in BEGIN
section setting FS
as ,
and OFS
as |
. In main program checking if 3rd column is matching findit then re-assigning 1st field to itself to apply OFS
basically to it and printing FNR
followed by current line value.