You need to add proper spaces!
With your erronous awk ' /^-/ {system("echo" $0 ">" "newline.txt")} '
, the shell command is essentially echo-xxxxxxxx()xxxxxxxx>newline.txt
, which surely doesn't work. You need to construct a proper shell command inside the awk
string, and obey awk
s string concatenation rules, i.e. your intended script should look like this (which is still broken, because $0
is not properly quoted in the resulting shell command):
awk '/^-/ { system("echo " $0 " > newline.txt") }'
However, if you really just need to echo $0
into a file, you can simply do:
awk '/^-/ { print $0 > "newline.txt" }'
Or even more simply
awk '/^-/' > newline.txt
Which essentially applies the default operation to all records matching /^-/
, whereby the default operation is to print
, which is short for neatly printing the current record, i.e. this script simply filters out the desired records. The > newline.txt
redirection outside awk
simply puts it into a file.