Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change display format of found result of awk

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

3 | 0 | L | "Third Job"

number 3 indicates that the result were found on the 3rd line.
How can I do this?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading