スクリプトを使用してフィールドで区切られたファイルから列を削除する

スクリプトを使用してフィールドで区切られたファイルから列を削除する

パイプ記号で区切られたファイル

マイファイル.txt

Hello|how|are|you|hope|you|are|doing|fine
Lilly|jasmine|rose|sunflower|nightfire|flowers

3より大きい列を削除したいです。結果セットが欲しい

Hello|how|are
Lilly|jasmine|rose

Unix シェルスクリプトで Unix コマンドを使用します。

ベストアンサー1

OFSおよび(出力)フィールド区切り文字を使用してFS最初の3列のみを印刷します。

$ awk  'BEGIN{OFS=FS="|"}{print $1,$2,$3}' file.txt
Hello|how|are
Lilly|jasmine|rose

sed を使用して最初の 2 つだけを|[everything that's not |]維持します。

~$ sed 's/\(\(|[^|]*\)\{2\}\).*/\1/' file.txt
Hello|how|are
Lilly|jasmine|rose

おすすめ記事