2016-07-07 62 views
0

我有在如下YAML文件數據:如何根據傳遞給腳本的值在腳本中打印內容?

report_id:youtube 
columns: 
    - date 
    - channel_id 
    - video_id 
    - claimed_status 
    - uploader_type 
    - live_or_on_demand 
    - subscribed_status 
report_id:device 
columns: 
    - date        
    - views        
    - comments       
    - shares        
    - channel_id       
    - watch_time_minutes 

並且許多這樣的report_ids和它的本列信息。

我已經編寫了一個腳本,根據在shell腳本中傳遞的report_id將列寫入另一個輸出文件。下面是一個例子:

copy_fields() 
{ 
report_id=`cat $FILE | grep 'report_id: device' | awk '{print $NF}'` 

echo "copy $report_id" > $OUTPUT 
echo "(" >> $OUTPUT 

for i in `cat $FILE | grep - | awk '{print $NF}'` 
do 
    echo $i 
     echo $i >> $OUTPUT 
done 
echo ")" >> $OUTPUT 
} 

copy_fields 

上面列應該只寫設備信息到文件「輸出」,但它寫的所有數據到「輸出」文件類似如下:

- date 
- channel_id 
- video_id 
- claimed_status 
- uploader_type 
- live_or_on_demand 
- subscribed_status 
- date        
- views        
- comments       
- shares        
- channel_id       
- watch_time_minutes 

腳本應該寫上REPORT_ID基礎數據到「輸出」文件中傳遞,如果設備通過它應該寫爲「輸出」,如下:

- date        
    - views        
    - comments       
    - shares        
    - channel_id       
    - watch_time_minutes. 

請HEL在此我可以得到確切的代碼。我需要編寫一個shell腳本,通過傳遞report_id從yaml文件中讀取數據,並能夠使用report_id的列將該數據寫入另一個輸出文件。

+0

顯示輸出應如何顯示的示例。 – 123

+0

如果REPORT_ID是設備,然後輸出應該如下: - 最新 - 觀點 - 評論 - 股 - CHANNEL_ID - watch_time_minutes 如果REPORT_ID是YouTube的輸出數據應該考慮: 日期 - CHANNEL_ID - VIDEO_ID - claims_status - uploader_type - live_or_on_demand - subscribed_status – sneha

+0

'awk'/ report_id:device/{x = 1} x &&/^ * - /'file'? – 123

回答

0

你可以試試下面的awk腳本

key='device' 
awk -v key=$key '{if(/report_id/){val=$0}else{if(val ~ key){FS=""; print $0}}}' input 

輸出

columns: 
    - date        
    - views        
    - comments       
    - shares        
    - channel_id       
    - watch_time_minutes 
+0

OP要求「不考慮report_id通過」 – Chet

+0

錯過了之前,現在更新 – Avikd

+0

它給所有其中的列:使用以下代碼: 'copy_fields() { key ='youtube' report_id = awk -v key = $ key'{if(/ report_id /){val = $ 0} else {if val_key){FS =「」;打印$ 0}}}」 回聲 「複製$ REPORT_ID」> $ OUTPUT 回聲 「(」 >> $ OUTPUT 因爲我在'貓$文件| grep的 - | awk的 '{打印$ NF}'' 做 \t回聲$我 回聲$ I >>做$ OUTPUT 回聲 「)」 >> $ OUTPUT }' copy_fields – sneha

0

試試這個AWK!

awk -v pat=$a 'c&&!--c{next};($0==pat){p=1;c=1;next} p&&/^report_id:/{p=0};p' file 

awk -v pat="report_id:whatever" 'c&&!--c{next};($0==pat){p=1;c=1;next} p&&/^report_id:/{p=0};p' file 

輸出:

- date 
    - views 
    - comments 
    - shares 
    - channel_id 
    - watch_time_minutes 

$ A = 「REPORT_ID:設備」 或 「REPORT_ID:YouTube」 的視頻或任何..可以在bash腳本

聲明
+0

OP要求在評論 「不論REPORT_ID傳遞」 – Chet

0

運行./script.sh設備文件輸出

Argumnet 1 REPORT_ID

參數2文件

參數3輸出文件

腳本。SH

#! /bin/bash 
report=$1 
file=$2 
awk -vrep="$report" 'BEGIN{rep="report_id:"rep} flag && /^report_id/{flag=0} $0 == rep{getline;getline;flag++} flag' $file > $3 

使用> $ 3覆蓋>> $ 3到附加輸出

輸出

- date 
    - views 
    - comments 
    - shares 
    - channel_id 
    - watch_time_minutes 

AWK解釋

BEGIN{rep="report_id:"rep} #concat variable to report_id 
flag && /^report_id/{flag=0} #reset print flag 
$0 == rep{getline;getline;flag++} #check for match, if matched the skip to 2nd line and set print flag 
flag #print the line 

EDIT

RUN:./script.sh設備

貓script.sh

#! /bin/bash 
report=$1 
FILE=report_type_id_list.yml 
OUTPUT=report_type_id_copy_fields.sh 
copy_fields() 
{ 
awk -vrep="$report" 'BEGIN{rep="report_id:"rep ;} flag && /^report_id/{flag=0} $0 == rep{getline;getline;flag++} flag' $FILE > $OUTPUT 

} 
copy_fields 

輸出

貓report_type_id_copy_fields.sh

- date 
    - views 
    - comments 
    - shares 
    - channel_id 
    - watch_time_minutes 
+0

謝謝你的評論,劇本運行但其輸出文件中不顯示輸出。 – sneha

+0

您是否運行了共享的相同輸入 – Chet

+0

FILE = report_type_id_list.yml#具有數據的文件 OUTPUT = report_type_id_copy_fields.sh#根據傳遞的report_id的選擇寫入輸出文件。 copy_fields() { report_id ='cat $ FILE | grep'report_id:youtube'| awk'{print $ NF}'' echo「copy $ report_id」> $ OUTPUT echo「(」>> $ OUTPUT for cat in FILE | grep - | awk'{print $ NF}' ' 做 \t回聲$我 回聲$ I >> $ OUTPUT 做 回聲 「)」 >> $ OUTPUT } copy_fields #can請您修改上面的腳本 – sneha

相關問題