Skip to content
Advertisement

Getting unexpected newline error while doing awk

I am running the following script in linux command line:

ps -ef| grep "java -Droute=full" | awk 'NR!=1{print $2}' | while read output;
do
 pid=$(echo $output)
 kill $pid
done

I am getting the following error:

awk: cmd. line:1: NR!=1{print
awk: cmd. line:1:            ^ unexpected newline or end of string

I believe the quotes and spaces are consistent, but why am i still getting this error?

Output of ps -ef | grep "java -Droute=full"

root     24441     1  0 Apr22 ?        00:00:00 sudo nohup java -Droute=full -jar abc
root     24442 24441  7 Apr22 ?        06:41:10 java -Droute=full -jar abc
user  30804 30771  0 05:27 pts/0    00:00:00 grep --color=auto java -Droute=full

Advertisement

Answer

The actual problem you’re trying to solve is something like “How to kill the process whose command line contains java -Droute=full.

A much easier, safer way is to just use pkill:

pkill -f 'java -Droute=full'

This replaces your entire script.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement