awk --> after IF-check appent a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk --> after IF-check appent a string
# 1  
Old 10-02-2013
awk --> after IF-check appent a string

Hi

i want to check the value in the last column and if this is greater than 180, awk should append a string - here i.e. !!! W !!!

Code:
awk '{IF $NF>180 ; print $0p"\t !!! W !!!"}' file

this code will give me the printout, but i want the change it in the file. Can someone give me a hint?

Thanks in advance
IMPe
# 2  
Old 10-02-2013
To change it in the file, with awk, you'll need to write the output to a temporary file, then copy that over the original.
This User Gave Thanks to Scott For This Post:
# 3  
Old 10-02-2013
Quote:
Originally Posted by Scott
To change it in the file, with awk, you'll need to write the output to a temporary file, then copy that over the original.
Thanks Scott for the fast reply.
When i copy it in a temp-file, what about the lines where $NF<180 is ?

IMPe
# 4  
Old 10-02-2013
Good point!

Try something like:
Code:
awk '$NF > 180 { $NF = $NF "\t !!! W !!!"}1' file > file.tmp && cp file.tmp file

This User Gave Thanks to Scott For This Post:
# 5  
Old 10-02-2013
Quote:
Originally Posted by Scott
Try something like:
Code:
awk '$NF > 180 { $NF = $NF "\t !!! W !!!"}1' file > file.tmp && cp file.tmp file

Great, thanks Scott!
Is working fine, but one question left for me to understand:
Code:
awk '$NF > 180 { $NF = $NF "\t !!! W !!!"}1' file > file.tmp && cp file.tmp file

What is the "1" at the end of the bracket for?

Last edited by Don Cragun; 10-02-2013 at 04:11 PM.. Reason: Add missing close CODE tag.
# 6  
Old 10-02-2013
It's a mixture of 'force of habit' and laziness!

It's equivalent to 1 {print} since print is the default action when a condition is true (1 being always true).

These are all the same:
Code:
1
{ print }
1 { print }

This User Gave Thanks to Scott For This Post:
# 7  
Old 10-02-2013
Quote:
Originally Posted by Scott
It's a mixture of 'force of habit' and laziness!

It's equivalent to 1 {print} since print is the default action when a condition is true (1 being always true).

These are all the same:
Code:
1
{ print }
1 { print }

ok!
Thanks a lot!
IMPe
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

4. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

5. Shell Programming and Scripting

Need HELP with AWK split. Need to check for "special characters" in string before splitting the file

Hi Experts. I'm stuck with the below AWK code where i'm trying to move the records containing any special characters in the last field to a bad file. awk -F, '{if ($NF ~ /^|^/) print >"goodfile";else print >"badfile"}' filename sample data 1,abc,def,1234,A * 2,bed,dec,342,* A ... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

7. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

8. Shell Programming and Scripting

check if one string comes before another

I want to search a file to check if there is string a before string b: string a, b might not necessarily be in the file, and to return a boolean string a (any text) string b i have tried for an hour to do this with perl, grep, etc. but no luck grep doesnt search for patterns... (7 Replies)
Discussion started by: vanessafan99
7 Replies

9. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies
Login or Register to Ask a Question