awk to search for specific line and replace nth column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to search for specific line and replace nth column
# 8  
Old 12-09-2013
Akshay

I tried
Code:
awk 'substr($1,length($1)-3) == 2013{$13 = -9.99; for (i=2; i<=NF; i++) $i=sprintf("%6.2f", $i)}1'

but it only does the last command not the first.
# 9  
Old 12-09-2013
Quote:
Originally Posted by ncwxpanther
Akshay

I tried
Code:
awk 'substr($1,length($1)-3) == 2013{$13 = -9.99; for (i=2; i<=NF; i++) $i=sprintf("%6.2f", $i)}1'

but it only does the last command not the first.
post what you are getting.
# 10  
Old 12-09-2013
Code:
AA00101062012   1.54   0.76  -1.39  -2.18  -2.59  -2.84  -2.28  -1.96  -1.02   1.42   0.61   0.73 -99.99
AA00101062013   1.36   0.75   0.65   0.95   1.22   1.21   2.90   3.08   2.94   2.22 1.59  -9.99 -99.99
AA00101062014 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99

# 11  
Old 12-09-2013
Quote:
Originally Posted by ncwxpanther
Code:
AA00101062012   1.54   0.76  -1.39  -2.18  -2.59  -2.84  -2.28  -1.96  -1.02   1.42   0.61   0.73 -99.99
AA00101062013   1.36   0.75   0.65   0.95   1.22   1.21   2.90   3.08   2.94   2.22 1.59  -9.99 -99.99
AA00101062014 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99 -99.99

what do you expect ?
# 12  
Old 12-09-2013
Code:
AA00101062012   1.54   0.76  -1.39  -2.18  -2.59  -2.84  -2.28  -1.96  -1.02   1.42   0.61   0.73 -99.99
AA00101062013   1.36   0.75   0.65   0.95   1.22   1.21   2.90   3.08   2.94   2.22  1.59  -9.99 -99.99

# 13  
Old 12-09-2013
Quote:
Originally Posted by ncwxpanther
Code:
AA00101062012   1.54   0.76  -1.39  -2.18  -2.59  -2.84  -2.28  -1.96  -1.02   1.42   0.61   0.73 -99.99
AA00101062013   1.36   0.75   0.65   0.95   1.22   1.21   2.90   3.08   2.94   2.22  1.59  -9.99 -99.99

Try:

Code:
$ awk 'substr($1,length($1)-3) <= 2013{$13 = -9.99; for (i=2; i<=NF; i++) $i=sprintf("%6.2f", $i);print}' file

This User Gave Thanks to Akshay Hegde For This Post:
# 14  
Old 12-09-2013
The 1 at the end of the script will print every single line, regardless of the modification of $13 has taken place or not. If you want to modify lines with 2013 only but want to print every line that has a year less or equal 2013, you need to introduce two conditiones/patterns, one for modification, one for print.

Last edited by RudiC; 12-09-2013 at 02:28 PM.. Reason: typo
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace Value of nth Column of Each Line Using Array

Hello All, I am writing a shell script with following requirement: 1. I have one input file as below CHE01,A,MSC,INO CHE02,B,NST,INC CHE03,C,STM,INP 2. In shell script I have predefined array as below: Array1={A, B, C} Array2= {U09, C04, A054} (6 Replies)
Discussion started by: angshuman
6 Replies

2. Shell Programming and Scripting

Search term in nth field and replace kth column

Hi, I have a text file which looks like this a.txt A,12,Apple,Red B,33,Banana,Yellow C,66,Sky,Blue I need to search for a particular field(s) in particular column(s) and for that matching line need to replace the nth column. Sample scenario 1: Search for 66 in second field and Sky in... (5 Replies)
Discussion started by: wahi80
5 Replies

3. Shell Programming and Scripting

awk search and replace nth column by using a variable.

I am passing a variable and replace nth value with the variable. I tried using many options in awk command but unable to ignore the special characters in the output and also unable to pass the actual value. Input : "1","2","3" Output : "1","1000","3" TempVal=`echo 1000` Cat... (2 Replies)
Discussion started by: onesuri
2 Replies

4. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

5. Shell Programming and Scripting

Search Replace Specific Column using RegEx

Have Pipe Delimited File: > BRYAN BAKER|4/4/2015|518 VIRGINIA AVE|TEST > JOE BAXTER|3/30/2015|2233 MockingBird RD|ROW2On 3rd column where the address is located, I want to add a space after every numeric value - basically doing a "s//&\ / ": > BRYAN BAKER|4/4/2015|5 1 8 VIRGINIA AVE|TEST > JOE... (5 Replies)
Discussion started by: svn
5 Replies

6. Shell Programming and Scripting

awk : search last index in specific column

I am trying to search a given text in a file and find its last occurrence index. The task is to append the searched index in the same file but in a separate column. I am able to accomplish the task partially and looking for a solution. Following is the detailed description: names_file.txt ... (17 Replies)
Discussion started by: tarun.trehan
17 Replies

7. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

8. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

9. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

10. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies
Login or Register to Ask a Question