2

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

Vijay
  • 65,327
  • 90
  • 227
  • 319

3 Answers3

0

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
  • 10,355
  • 2
  • 46
  • 60
  • 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 Jul 22 '10 at 06:44
0
awk -vvar="hot" '{gsub("cool",hot,t);print t}{t=$0}END{print}' file
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

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
  • 238,783
  • 38
  • 220
  • 352