0

应查尔斯·达菲(Charles Duffy)的要求以更窄的焦点重新创建此内容。

我有一个如下所示的 CSV 文件:

Security Policy: Blahblahblah,,,,,,,,,
12,,host_A,net-B,https,drop,Log,Any,Any,comments
13,,host_A,net-B,smtp,drop,Log,Any,Any,comments
14,,host_A,net-B,http,accept,Log,Any,Any,comments 
,,net-C,,,,,,,
,,net-D,,,,,,,
15,,host_A,net-B,http,accept,Log,Any,Any,comments
,,host_B,net-C,service_X,,,,,
,,host_C,net-D,service_y,,,,,
,,host_D,,,,,,,
,,host_E,,,,,,,

我需要分别解析每个值,但是我需要在各自的语句中包含 $1。如您所见,这对于 13 和 14 来说很容易,但是当 14 和 15 的列为空白(子级)时,这将成为一个严重的问题。

循环这个最好的方法是什么?

例如,我希望输出看起来像:

'text goes here' $1 'more text' $3 'more text'
'text goes here' $1 'more text' $4 'more text'

等等

使用实际值(对于 15):

'text goes here' 15 'more text' host_A 'more text'
'text goes here' 15 'more text' host_B 'more text'
'text goes here' 15 'more text' host_C 'more text'
'text goes here' 15 'more text' host_D 'more text'
'text goes here' 15 'more text' host_E 'more text'
'text goes here' 15 'other text' net-B 'more text'
'text goes here' 15 'other text' net-C 'more text'
'text goes here' 15 'other text' net-D 'more text'
'text goes here' 15 'text' http 'more text'
'text goes here' 15 'text' service_X 'more text'
'text goes here' 15 'text' service_y'more text'

等等等等。

谢谢,

4

2 回答 2

1

我真的在猜测,因为您的问题并没有很好地解释您想要什么,但是您正在寻找的是这样的东西:

$ cat file
Security Policy: Blahblahblah,,,,,,,,,
12,,host_A,net-B,https,drop,Log,Any,Any,comments
13,,host_A,net-B,smtp,drop,Log,Any,Any,comments
14,,host_A,net-B,http,accept,Log,Any,Any,comments
,,net-C,,,,,,,
,,net-D,,,,,,,
15,,host_A,net-B,http,accept,Log,Any,Any,comments
,,host_B,net-C,service_X,,,,,
,,host_C,net-D,service_y,,,,,
,,host_D,,,,,,,
,,host_E,,,,,,,

$ awk -f tst.awk file
12  host_A net-B https drop Log Any Any comments
13  host_A net-B smtp drop Log Any Any comments
14  host_A net-B http accept Log Any Any comments
14  net-C net-B http accept Log Any Any comments
14  net-D net-B http accept Log Any Any comments
15  host_A net-B http accept Log Any Any comments
15  host_B net-C service_X accept Log Any Any comments
15  host_C net-D service_y accept Log Any Any comments
15  host_D net-B http accept Log Any Any comments
15  host_E net-B http accept Log Any Any comments

$ cat tst.awk
BEGIN{ FS="," }
NR==1 { next }
$1 != "" { split($0,dflt) }
{ for (i=1;i<=NF;i++) if ($i == "") $i = dflt[i]; print }
于 2013-04-03T17:17:46.250 回答
1
process_file() {
  # declare local variables
  declare -a last_seen row
  declare col_idx col

  read # discard first line

  while IFS=, read -r -a row; do
    # for each remaining line...
    for col_idx in "${!row[@]}"; do
      # update last-seen rows with any contents here
      col=${row[$col_idx]}
      if [[ $col ]]; then
        last_seen[$col_idx]=$col
      fi
    done

    # ...and operate on those values.
    printf 'text goes here %s more text %s more text\n' \
      "${last_seen[0]}" "${last_seen[2]}" "${last_seen[3]}"
  done
}

process_file <input.csv >output.txt
于 2013-04-03T16:21:03.270 回答