中間ファイルを作成せずに複数の文字列を連結する

中間ファイルを作成せずに複数の文字列を連結する

一部のファイルの一部を抽出して別のファイルにリンクしたいのですが、中間ファイルを作成したくありません。

たとえば、

$ cat textExample.txt 
Much I marvelled this ungainly fowl to hear discourse so plainly,
Though its answer little meaning- little relevancy bore;
For we cannot help agreeing that no living human being
Ever yet was blessed with seeing bird above his chamber door-
Bird or beast upon the sculptured bust above his chamber door,
With such name as "Nevermore."
$ cat textExample.txt | tr -d "\n" | awk 'NR==1' | awk '{print substr($0, 8, 9)}'
marvelled
$ cat textExample.txt | tr -d "\n" | awk 'NR==1' | awk '{print substr($0, 77, 6)}'
answer
$ cat textExample.txt | tr -d "\n" | awk 'NR==1' | awk '{print substr($0, 189, 7)}'
blessed

文を結合するには、ファイルを作成します。

$ cat textExample.txt | tr -d "\n" | awk 'NR==1' | awk '{print substr($0, 8, 9)}'| tr "\n" " " > intermediate.txt
$ cat textExample.txt | tr -d "\n" | awk 'NR==1' | awk '{print substr($0, 77, 6)}' | tr "\n" " " >> intermediate.txt
$ cat textExample.txt | tr -d "\n" | awk 'NR==1' | awk '{print substr($0, 189, 7)}' >> intermediate.txt
$ cat intermediate.txt 
marvelled answer blessed

または、複数のawkコマンドを使用できます(ただし、改行文字を削除することはできません)。

$ cat textExample.txt | tr -d "\n" | awk 'NR==1' | awk '{print substr($0, 8, 9)}; {print substr($0, 77, 6)}; {print substr($0, 189, 7)}' 
marvelled
answer
blessed

cat次のようなものを使用して、中間ファイルに依存せずに他の単語を一緒にリンクできるかどうか疑問に思います。

$ cat {first word} | cat {second word} | cat {third word} 
first second third

ありがとう

ベストアンサー1

私があなたを正しく理解したならば、これは私にとって効果的でした。

cat textExample.txt | tr -d "\n" | awk '{print substr($0, 8, 9) " " substr($0, 77, 6) " " substr($0, 189, 7)}'

おすすめ記事