tags:

views:

45

answers:

3

i tried the awk liner below(on windows command-prompt):not working properly

gawk -v var="hot" "{ if(!NR){gsub(/cool/,var,$0) ;print} else{print}}" awk_test

input file is below

 this is a cool jack
 this nota cool kack
 this obviously a cool jack 

a unix solution is also feasible

A: 

You could try something like:

sed '$d' yourfile | sed 's/cool/hot/g' > newfile
tail -1 yourfile >> newfile

this should do the substitution on everything but the last line of the file first, and then add the last line of the original file.

tonio
this is ok ...but i dont have sed in my command prompt.i only have gawk.so i need an awk/gawk solution. what is actually wrong with my gawk script.
Vijay Sarathi
A: 
awk -vvar="hot" '{gsub("cool",hot,t);print t}{t=$0}END{print}' file
ghostdog74
A: 

You could also pass the number of lines as a parameter:

gawk -v var=hot -v lines=$(wc -l < test.txt) '
    NR != lines {gsub(/cool/, var)} 
    {print}
' test.txt
glenn jackman