
テキストファイルがありますFilenr.lis、含まれる
# 1 2016-05-31-1003-57S._BKSR_003_CM6
# 2 2016-06-01-2255-54S._BKSR_003_CM6
# 3 2016-06-05-1624-57S._BKSR_003_CM6
# 4 2016-06-07-1914-55S._BKSR_003_CM6
.
.
.
など。
私の出力は次のようになります
2016-05-31-10-03
2016-06-01-22-55
2016-06-01-22-55
2016-06-07-19-14
これを試しましたが、その形式はありません。
awk -F'-' '{print "2016""-"$2"-"$3"-"$4}' filenr.lis
答え1
アッ
awk '{print substr($3,0,13)"-"substr($3,14,2)}' file.txt
2016-05-31-10-03
2016-06-01-22-55
2016-06-05-16-24
2016-06-07-19-14
sed
sed 's/^......\(.............\)\(..\).*/\1-\2/' file.txt
sed、しかしもう少しスマートになった
sed 's/^.\{6\}\(.\{13\}\)\(..\).*/\1-\2/' file.txt
真珠
perl -pe 's/^.{6}(.{13})(..).*/$1-$2/' file.txt
答え2
固定列、固定サイズ、固定文字位置に基づく切断方法:
$ cut --output-delimiter='-' -c7-19,20-21 file.txt
# display from char 7 up to 19, then print output delimiter, then display from char 20 up to char 21.
すごい解決策:
$ while IFS= read -r line;do line="${line:6:13}-${line:14:2}";echo $line;done<file.txt
文字の代わりにフィールドベースのソリューション:
while IFS= read -r line;do
line=$(cut -d' ' -f5- <<<"$line") #with space delimiter get field 5 up to the end
line=$(cut -d- -f1-4 <<<"$line") #with delimiter="-" get field 1 up to 4
line=$(sed "s/${line: -2}/-${line: -2}/g" <<<"$line") #insert a dash before last two characters
echo "$line"
done<file
プロセス置換を含む1行のコード:
$ sed 's/..$/-\0/g' <(cut -d- -f1-4 <(cut -d" " -f5- file.txt)) #use >newfile at the end to send the results to a new file
# 1
すべての場合において、結果は入力ファイルを考慮して予想通りです(各行の先頭を含む)。
答え3
個人的に好きなのはawkソリューションですが、ここに別のアプローチがあります。
cat FILE_NAME | tr -s ' ' | cut -d' ' -f3 | cut -b 1-13