Need a Linux command for find/replace column based on specific criteria.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a Linux command for find/replace column based on specific criteria.
# 1  
Old 04-05-2017
Need a Linux command for find/replace column based on specific criteria.

I'm new to shell programming, I have a huge text file in the following format, where columns are separated by single space:
Code:
ACA MEX 4O_ $98.00 $127.40 $166.60 0:00 0:00 0 ;
ACA YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ;
ACA YYZ TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ;
ADZ YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ;
ADZ YYZ TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ;
AGP AMS HV_ $441.00 $573.30 $749.70 0:00 0:00 0 ;
AGP AMS VY_ $235.00 $305.50 $399.50 0:00 0:00 0 ;

I want to replace all values in the 5th column by $-1.00 only when the third column is equal to "TS_". Basically the result of transforming the following line:
Code:
ACA YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ;

Would be:

Code:
ACA YUL TS_ $300.00 $-1.00 $510.00 0:00 0:00 0 ;

Is there any easy way to do it via AWK command ?

Please advise.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-05-2017 at 03:47 PM.. Reason: Added CODE tags.
# 2  
Old 04-05-2017
Try
Code:
awk '{ if ($3 == "TS_") $5 = "$-1.00" ; print }' input >output

This User Gave Thanks to hergp For This Post:
# 3  
Old 04-05-2017
Or
Code:
awk '$5=($3=="TS_")?"$-1.00":$5' file

assuming $5 will never be empty nor zero.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-06-2017
that works perfectly. Thank you guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print specific line in file based on criteria

In the file below I am trying to extract a specific instance of path, if the adjacent plugin": "/rundb/api/v1/plugin/49/. Thank you :). file "path": "/results/analysis/output/Home/Auto_user_S5-00580-4-Medexome_65_028/plugin_out/FileExporter_out.52", "plugin": "/rundb/api/v1/plugin/49/",... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

Help with replace specific column command

Input file: ASD_QAW 12 A_@ AE_AQ 21 PA_123 ASDA_@ 23 ADA_AS . . Output file: ASD_QAW 12 A @ AE_AQ 21 PA 123 ASDA_@ 23 ADA AS . . Do anybody know how to just specific and replace "_" in column 3 with tab delimiter (\t)? Thanks for advice. (2 Replies)
Discussion started by: perl_beginner
2 Replies

3. Shell Programming and Scripting

Extract error records based on specific criteria from Unix file

Hi, I look for a awk one liner for below issue. input file ABC 1234 abc 12345 ABC 4567 678 XYZ xyz ght 678 ABC 787 yyuu ABC 789 7890 777 zxr hyip hyu mno uii 678 776 ABC ty7 888 All lines should be started with ABC as first field. If a record has another value for 1st... (7 Replies)
Discussion started by: ratheesh2011
7 Replies

4. Shell Programming and Scripting

Find and replace string matching criteria

Dear Friends, I am looking for a way to replace a string (multiple lines) starting with something and ending with something (these two values do not change) with blank. Basically I want to delete this code injection accross many sites and folders. Search Code (across files and folders) that... (2 Replies)
Discussion started by: libras
2 Replies

5. Shell Programming and Scripting

Extract data based on specific search criteria

I have a huge file (about 2 millions records) contains data separated by “,” (comma). As part of the requirement, I can't change the format. The objective is to remove some of the records with the following condition. If the 23rd field on each line start with 302 , I need to remove that from the... (4 Replies)
Discussion started by: jaygamini
4 Replies

6. UNIX for Dummies Questions & Answers

AWK Command to find text in specific column

I'm new to scripting and would appreciate any help. I have a list of over 20 words in File1 that I need to find in columns 10-15 of File2. I need the entire row of File2 that the File1 list matches. I originally used a grep command which works, but provides File1 results that can be found... (3 Replies)
Discussion started by: Chillin
3 Replies

7. Shell Programming and Scripting

Need an awk for a global find/replace in a file, specific column

I am new to unix and awk/sed etc... using C-Shell. Basically, I have a fixed length file that has 4 different record types on it, H, D, V, W all in column 1. I need to change all the W's in column 1 to D's. in the entire file. The W's can be anywhere in the file and must remain in the same... (3 Replies)
Discussion started by: jclanc8
3 Replies

8. Shell Programming and Scripting

substract column based on some criteria

Please guide if you know how to solve this. I have a tab delimited INPUT FILE where each record is separated by ----- ----- ABC 4935402 4936680 Pattern=Cheers07080.1 ABC 4932216 4932368 Pattern=Cheers07080.1 ABC 4931932 4932122 ... (8 Replies)
Discussion started by: sam_2921
8 Replies

9. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

10. UNIX for Dummies Questions & Answers

Select records based on search criteria on first column

Hi All, I need to select only those records having a non zero record in the first column of a comma delimited file. Suppose my input file is having data like: "0","01/08/2005 07:11:15",1,1,"Created",,"01/08/2005" "0","01/08/2005 07:12:40",1,1,"Created",,"01/08/2005"... (2 Replies)
Discussion started by: shashi_kiran_v
2 Replies
Login or Register to Ask a Question