如何只获得一个文件中一行的出现

时间:2016-02-15 05:46:47

标签: shell

我有一个文件raw-vobs-config-spec,其中有两行

element /vob/ccm_tpl/repository/open_source/ciscossl_fom/4_1/... TPLBASE
element /vob/ccm_tpl/repository/open_source/ciscossl/1_0_2d_5_4/... VERSION_04

我有我的代码:

OLD_VERSION=`grep "ciscossl" raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print $(NF-1)}'`
echo $OLD_VERSION
total_fields=`grep "ciscossl" raw-vobs-config-spec | cut -d " " -f2 | awk -F "/" '{ print NF }'`
echo $total_fields
#directory_path=`grep "ciscossl" raw-vobs-config-spec | cut -d " " -f2 | cut -d"/" -f1-"${total_fields}"`
#echo $directory_path
loc=`grep "ciscossl" raw-vobs-config-spec_new | cut -d " " -f2 | cut -d"/" -f1-6`
echo $loc

所以它打印o / p为

4_1 1_0_2d_5_4
8 8
/vob/ccm_tpl/repository/open_source/ciscossl_fom 
/vob/ccm_tpl/repository/open_source/ciscossl

但我需要输出为

4_1
8
/vob/ccm_tpl/repository/open_source/ciscossl_fom 

我怎么能得到它?

1 个答案:

答案 0 :(得分:0)

您可以使用read而不使用while循环首先只读取一行,然后使用awk处理读取的文本:

# read one line from input file
read _ line _ < raw-vobs-config-spec

# process the line with awk
echo "$line" | awk 'BEGIN{FS=OFS="/"} {print $(NF-1) ORS NF; NF-=2; print}'

<强>输出:

4_1
8
/vob/ccm_tpl/repository/open_source/ciscossl_fom