awk command to search based on 5 user input fields

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command to search based on 5 user input fields
# 1  
Old 12-05-2017
Question awk command to search based on 5 user input fields



awk -F , '{ if ( $3 == Field1 && $6 == Field2 && $8 == Field3 && $9 == Field4 && $10 == Field5) print $0 }' /tmp/rodney.outD

INPUT FILE (Rodney.outD):

prd_care_d,CARE_PD,prdensp117,,2017-11-11,SCMSProcess,129,Stream: R2017.11.11,Thu Nov 09 13:22:44 PST 2017,RUNNING,CARE_PD-SCMSProcess-2017-11-11-SCMS_Process_Archive-117A.tra,2017-11-28 19:00,,14370,11-11$
  prd_care_d,CARE_PD,prdensp117,,2016-11-16,SerializationProcess,242,Stream: R2016.11.15,Tue Nov 15 10:21:57 PST 2016,RUNNING,CARE_PD-SerializationProcess-2016-11-16-Serialization_Process_Archive-117A.tra,2017-11-28 19:00,,30765,08-09$
  prd_care_d,CARE_PD,prdensp117,,2017-11-15,TugsServiceLoggerProcess,14,Stream: R2017.11.14,Wed Nov 15 22:50:49 PST 2017,RUNNING,CARE_PD-TugsServiceLoggerProcess-2017-11-15-Tugs_ServiceLogger_Process_Archive-117A.tra,2017-11-28 19:00,,12369,11-16$
  prd_care_d,CARE_PD,prdensp117,,2017-01-21,VestaProcess,161,Stream: R2017.01.21,Thu Jan 19 09:37:40 PST 2017,RUNNING,CARE_PD-VestaProcess-2017-01-21-Vesta_Process_Archive-117A.tra,2017-11-28 19:00,,31468,08-09$

Basically what I need is a way to front load each of the 5 fields with a value that will act like a pass thru value on my awk statement. This way I can use the same "==" operator regardless of what the user inputs.

Thanks in advance.

Last edited by jim mcnamara; 12-05-2017 at 04:03 AM..
# 2  
Old 12-05-2017
Good example, but I do not get what you want. Can you create some dummy user input data? .... with the expected output. Do a all 5 field variables have to be non-empty?
# 3  
Old 12-05-2017
Wrench The input is included in the "," delimited output that - hopefully - showed up on my post

The 5 fields that are input by the user can be 4 or 3 or 2 or 1 field. That is the "rub". If the user only enters 1 field then the awk command must still have the other 4 fields in it. So I think. I added the -v option at the front of my awk statement so now I am able to pass variables into my command.
# 4  
Old 12-05-2017
Aside coment: in shell, make sure the variables' definitions are enclosed in " (oct 042, dec 34, hex 22), not the locale dependent unicode three byte characters used in post#1. On top, take care to correctly terminate lines (<NL>, \n, ^J, 0x0A only)!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to clean up input file, printing both fields

In the f1 file below I am trying to clean it up removing lines the have _tn_ in them. Next, removing the characters in $2 before the ninth /. Then I remove the ID_(digit- always 4). Finally, the charcters after and including the first _. It is curently doing most of it but the cut is removing $1... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Grep command to search pattern corresponding to input from user

One more question: I want to grep "COS_12_TM_4 pattern from a file look likes : "COS_12_TM_4" " ];I am taking scan_out as the input from the user. How to search "COS_12_TM_4" in the file which is corresponds to scan_out (12 Replies)
Discussion started by: Preeti Chandra
12 Replies

3. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

4. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

5. UNIX for Dummies Questions & Answers

Split files based on user input

Hi All, I have a UNIX script which reads "PxyType" (read PxyType) as input from user and a file (eg : "File.json") with the list all PxyType's. Based on the user input for "PxyType" in "File.json", I want to redirect each matched line to a different file ("File1,2,3,..json"). Can you... (7 Replies)
Discussion started by: Deena1984
7 Replies

6. Shell Programming and Scripting

How to preserve spaces in input fields with awk?

I'm trying to do something pretty simple but its appears more complicated than expected... I've lines in a text file, separated by the comma and that I want to output to another file, without the first field. Input file: file1,item, 12345678 file2,item, 12345678 file2,item, ... (8 Replies)
Discussion started by: Armoric
8 Replies

7. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

8. Shell Programming and Scripting

Search on date range of file based on user input

Hello I would like to ask for help with a script to search a directory that contains many log files and based on a users input after being prompted, they enter a date range down to the hour which searches the files that contain that range. I dont know how to go about this. I am hoping that the... (5 Replies)
Discussion started by: lostincashe
5 Replies

9. UNIX for Dummies Questions & Answers

Validating input based on fixed number of fields

Yes, i did... let me state my problem in more detail Inputs: I have one input CSV file And, i have stored no. of comma each line should in a variable. e.g. $ cat cmt.csv this, is a ,comma ,count test1 ,,this, is a ,comma ,count test2 this, is a ,comma ,count test3... (6 Replies)
Discussion started by: Dipali
6 Replies

10. Shell Programming and Scripting

search file, change existing value based on input (awk help)

I have a file (status.file) of the form: valueA 3450 valueB -20 valueC -340 valueD 48 I am tailing a data.file, and need to search and modify a value in status.file...the tail is: tail -f data.file | awk '{ print $3, ($NF - $(NF-1)) }' which will produce lines that look like this: ... (3 Replies)
Discussion started by: nortonloaf
3 Replies
Login or Register to Ask a Question

Featured Tech Videos