You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行含多引号的远程SSH命令时遇awk语法错误求助

Fixing Your awk Syntax Error and Command Issues

Hey there, let's walk through what's causing that syntax error and fix up your command properly.

What's Going Wrong

  1. Shell is eating your awk field references
    You wrapped the entire ssh command in double quotes, so your shell is interpreting $2 and $5 as shell variables (not awk's field identifiers). If those variables aren't defined in your shell, they get replaced with empty strings—leaving awk with print ,open_, which is invalid syntax.

  2. Missing input file for grep
    Your grep -i 20-10-2018 doesn't specify which file to search. It's waiting for input from stdin instead of reading result.csv directly.

  3. Syntax mistake in the while loop
    The while read line do part is missing a semicolon (or newline) between line and do—shell syntax requires a separator there.

  4. Undefined $Date variable
    Unless you've explicitly set a Date variable in your shell, $Date will output nothing. You should use the date command to get the current date instead.

Fixed Command

Here's the corrected version that addresses all these issues:

ssh "$ip_address" "grep -i 20-10-2018 result.csv | awk -F ',' '{print \$2\",open_\"\$5}'" | while read line; do echo "$(date +%Y-%m-%d):00:00,$line"; done

Key Fixes Explained

  • Escaped awk fields: We added backslashes to \$2 and \$5 so the shell passes them through to awk instead of trying to expand them. Alternatively, you could wrap the awk script in single quotes entirely (which is often cleaner):
    ssh "$ip_address" "grep -i 20-10-2018 result.csv | awk -F ',' '\''{print $2",open_"$5}'\''"
    
  • Moved result.csv to grep: Now grep reads directly from the file instead of waiting for input.
  • Fixed while loop syntax: Added a semicolon ; between line and do.
  • Replaced $Date with date command: $(date +%Y-%m-%d) generates a properly formatted date (adjust the format string like %d-%m-%Y if you need day-month-year instead).

Bonus: More Efficient Version

You can combine grep and awk into a single command to avoid extra pipe overhead:

ssh "$ip_address" "awk -F ',' '/20-10-2018/i {print \$2\",open_\"\$5}' result.csv" | while read line; do echo "$(date +%Y-%m-%d):00:00,$line"; done

The /20-10-2018/i in awk does the same case-insensitive match as your grep command.

内容的提问来源于stack exchange,提问作者KaverappaKU

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:56:33