Using awk to multiple and replace in a specific line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using awk to multiple and replace in a specific line
# 1  
Old 11-28-2014
Display Using awk to multiple and replace in a specific line

Hi Folks,

I have the file in which I need to multiply the content of a line and replace the initial content of that line with the obtained answer.
For example if this is my input file file1.txt
Code:
2.259314750       xxxxxx
1.962774350       xxxxxx
2.916817290       xxxxxx
1.355026900       xxxxxx
1.677389740       xxxxxx
3.47428338         xxxxxx

The output I am looking for is file2.txt
Code:
2.259314750       xxxxxx
1.962774350       xxxxxx
2.800144598       xxxxxx
1.355026900       xxxxxx
1.677389740       xxxxxx
3.47428338         xxxxxx

I know that using
Code:
awk '{if(NR==3) print $1*0.96}' file1.txt

but the results will be printed on the screen and is rounded up to
Code:
2.80014

instead of having
Code:
2.800144598

in line 3 of the output file.
Using
Code:
awk '{if(NR==3) printf ".9f", $1*0.96}' file1.txt

will print the whole answer but on screen also.
The issue I have is how to print the answer on the output file using an awk statement.

Thanks
# 2  
Old 11-28-2014
Try
Code:
awk 'NR==3 {$1*=0.96} 1' CONVFMT="%.9f" OFS="       " file
2.259314750       xxxxxx
1.962774350       xxxxxx
2.800144598       xxxxxx
1.355026900       xxxxxx
1.677389740       xxxxxx
3.47428338        xxxxxx

or:
Code:
awk 'NR==3 {$1=sprintf ("%.9f", $1*0.96)} 1'  OFS="       " file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-28-2014
awk doesn't do in-line editing. The 'poor-man' hack for that might be:
Code:
{ rm file1.txt; awk '{if(NR==3) printf ".9f", $1*0.96}' > file1.txt;} < file1.txt

# 4  
Old 11-28-2014
Try:
Code:
awk 'NR==3{sub($1,sprintf("%.9f",$1*0.96))}1' file1.txt > file1_new.txt


Last edited by Scrutinizer; 11-28-2014 at 01:30 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 11-28-2014
Thanks alot, worked the way I wanted it to

---------- Post updated at 12:34 PM ---------- Previous update was at 12:33 PM ----------

Quote:
Originally Posted by RudiC
Try
Code:
awk 'NR==3 {$1*=0.96} 1' CONVFMT="%.9f" OFS="       " file
2.259314750       xxxxxx
1.962774350       xxxxxx
2.800144598       xxxxxx
1.355026900       xxxxxx
1.677389740       xxxxxx
3.47428338        xxxxxx

or:
Code:
awk 'NR==3 {$1=sprintf ("%.9f", $1*0.96)} 1'  OFS="       " file

Thanks, works perfect
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

2. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

3. Shell Programming and Scripting

Multiple Line awk search and replace

I have a log file that contains many lines but contains the following line three times: related_pin : "t_bypass"; Here are the 3 occurrences and the two lines after from my file.txt: related_pin : "t_bypass"; sdf_cond : "rstq_b"; timing_sense : negative_unate; ... (6 Replies)
Discussion started by: bobbygb2003
6 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. 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

6. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

7. Shell Programming and Scripting

awk multiple-line search and replace one-liner

Hi I am trying to search and replace a multi line pattern in a php file using awk. The pattern starts with <div id="navbar"> and ends with </div> and spans over an unknown number of lines. I need the command to be a one liner. I use the "record separator" like this : awk -v... (8 Replies)
Discussion started by: louisJ
8 Replies

8. Shell Programming and Scripting

Replace text in SPECIFIC multiple files

I have a list of files with different file names and ext that i need replace(saved as file_list.txt.) i just want to replace from a specific file list. i obtain an error saying lint not found. Plz help. Thx perl -e "s/$NAME/$T_NAME/gi;" -pi $(find . -type f | xargs -0 lint -e < file_list.txt) (0 Replies)
Discussion started by: yvmy
0 Replies

9. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

10. Shell Programming and Scripting

How to replace specific text line out of multiple occurance

Hi I would like to replace specific line eg ExitAction = NONE to ExitAction = FALSE under only TASK sipsiproc and other ExitAction = NONE will remain as usual in the file(shell script) The file contains: TASK rgcdproc { CommandLine = $SSHOME/bin/rgcd.exe NewConsole... (5 Replies)
Discussion started by: madhusmita
5 Replies
Login or Register to Ask a Question