shell 腳本中的標題列驗證

“|” 分隔的文件應具有以下列標題

Activity
Activity+ ID
Description
Status

上傳後,在開始使用 SQLLDR 處理文件之前,我確保上傳的文件具有準確數量的標題,標題名稱匹配且順序相同。

代碼:

declare -i header=4
fields=( 
"Activity"
"Activity+ ID"
"Description"
"Status"
)

for i in "Test File.csv"; do
    read -r line < "$i" 

    oldIFS="$IFS"
    IFS=$'|'
    fldarray=( $line );
    IFS="$oldIFS"

    nfields=${#fldarray[@]}     
    if (( nfields < header ))
    then
    printf "error: only '%d' fields in file '%s'\nmissing:" "$nfields" "$i"
    else        
        for item1 in "${header[@]}"; do
          for item2 in "${fields[@]}"; do
           if [[ $item1 != $item2 ]]; then
            Array3+=("$item1")
           fi
         done
        done
        echo "not matching" ${Array3[@]}
        printf "\n\n"
    fi
done

數據:

Activity|Activity+ ID|Description|Status
Test|1234|First activity|Open

這總是打印缺少 Activity+ 列,儘管它存在於文件中。從頭文件和上傳的文件中刪除“+”後,它按預期工作。如何更改上述代碼以使用“+”驗證列標題。我參考了bash的答案來識別和驗證文件頭來構建這個解決方案

標記融合

注意:對於 OP 想要做什麼仍然有點困惑(例如,header被定義為整數但後來被引用為數組 ( "${header[@]}"))

假設:

  • 如果文件|第一行中的分隔字段.csv數與fields[]數組中的條目數不匹配,則打印錯誤
  • 文件中的標頭字段.csv必須與fields[]數組中的條目完全匹配(拼寫和順序)
  • 打印fields[]數組中與文件|第一行中的分隔字段不完全匹配的條目.csv

我們將保留當前fields[]數組:

fields=("Activity" "Activity+ ID" "Description" "Status")

.csv文件的第一行拉headers[]數組:

IFS='|' read -r -a headers < test.csv      # read first line from test.csv, break on '|' delimiter, store in headers[] array

給我們:

$ typeset -p fields headers
declare -a fields=([0]="Activity" [1]="Activity+ ID" [2]="Description" [3]="Status")
declare -a headers=([0]="Activity" [1]="Activity+ ID" [2]="Description" [3]="Status")

現在對OP的if/else/for/fi代碼進行一些修改

if [[ "${#fields[@]}" -ne "${#headers[@]}" ]]            # field count mismatch?
then
     echo "error: field count mismatch: expecting ${#fields[@]} / found ${#headers[@]}"
else
    Array3=()                                            # init array Array3[]

    for ((i=0;i<${#fields[@]};i++))                      # loop through indices of fields[] array
    do
        [[ "${fields[$i]}" != "${headers[$i]}" ]] && \   # if same position in both arrays is not a match then ...
        Array3+=("${fields[$i]}")                        # add fields[] entry to Array3[]
    done

    [[ "${#Array3[@]}" -ne 0 ]] && \                     # if Array3[] not empty then ...
    echo "not matching:" ${Array3[@]}                    # print list of fields to stdout
fi

對於這種特殊情況,當${fields[@]}${headers[@]}相同時,不會生成任何輸出。

其他測試用例:

headers[] 中的第二個字段拼寫不同

declare -a fields=([0]="Activity" [1]="Activity+ ID" [2]="Description" [3]="Status")
declare -a headers=([0]="Activity" [1]="Activity+" [2]="Description" [3]="Status")

# the code generates:

not matching: Activity+ ID

headers[] 有 3 個條目

declare -a fields=([0]="Activity" [1]="Activity+ ID" [2]="Description" [3]="Status")
declare -a headers=([0]="Activity" [1]="Activity+ ID" [2]="Status")

# the code generates:

error: field count mismatch: expecting 4 / found 3

headers[] 有 4 個條目,但都與 fields[] 中的相應條目不同

declare -a fields=([0]="Activity" [1]="Activity+ ID" [2]="Description" [3]="Status")
declare -a headers=([0]="Activity+ ID" [1]="Description" [2]="Status" [3]="Activity")

# the code generates:

not matching: Activity Activity+ ID Description Status

從這裡 OP 應該能夠調整代碼以提供所需的輸出和/或設置一些變量以用於後續條件操作(例如,如果echo觸發任何一個中止處理,如果觸發任何一個則禁用後續處理echo等)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

想在 shell 腳本的文本文件中添加標題

在 Shell 腳本中捲曲可能在標題部分失敗

在 Shell 腳本中匹配列和行

Apps 腳本:html 文件中的數據驗證

如何在shell腳本中組合exec命令

如何在 shell 腳本中運行 sacct 命令

我的 GitHub 操作中嵌入式 shell 腳本的 YAML 語法有什麼問題?

Ansible 調用執行帶有特定標籤的 shell 腳本

Power Shell 簡單腳本

如何在spark中將雙大括號從shell腳本傳遞到python腳本?

我需要在腳本執行結束時更新 shell 腳本中的源配置文件

我在將 bash 命令轉換為 shell 腳本中的 shell 腳本語法錯誤時遇到錯誤

如何通過讀取 shell 腳本中的 csv 文件將 2 列的總和添加到新列中

如何使用shell腳本在csv文件的列開頭插入值

正則表達式匹配 shell 腳本中數組中的數字

如何使用shell腳本解壓縮目錄中的所有文件?

當在子shell腳本中運行時,scp沒有列出

在shell腳本中將4位數年份轉換為2位數

循環變量不會傳遞給 Jenkins 中的 shell 腳本

在 bash/shell 腳本中測試“null”不起作用

從 shell 腳本中的文件讀取時缺少字符

用於循環的 shell 腳本中的多個條件

'DEBIAN_FRONTEND=noninteractive' 在 apt-get 的 shell 腳本中不起作用

無法使用“查找”在 shell 腳本中設置變量

無法在 shell 腳本的 gremlin 查詢中傳遞變量

如何在 Shell 腳本中傳遞命名參數

為什麼我的 if 語句在 shell 腳本中不起作用?

當腳本位於 shell 的 stdin 上時,apt-get 之後的 shell 腳本中的命令未正確執行

如何使用自定義文件夾中的所有腳本,如 macOS 中的本機 shell 命令