0

I would like to extract parts of some files and concatenate them into another but without writing an intermediate file.

For instance:

$ 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

In order to concatenate the sentences together, one file could be written:

$ 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

or multiple awk commands could be used (although I could not remove the newline):

$ 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

I was wondering whether cat could be used directly to concatenate the different words together without relying on an intermediate file, something like:

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

Thank you

2
  • How do you know what words to pick out? Is it the third word on every line (maybe not, you're skipping the third line)?
    – Kusalananda
    Apr 10, 2018 at 10:54
  • Please show what output you want to see. Apr 10, 2018 at 13:26

3 Answers 3

0

This works for me if I understand you correctly:

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

I don't get what you intend.

Yet try :

 ... | tr -d '\n' |
awk '{printf "%s %s %s\n", substr($0, 8, 9),substr($0, 77, 6),substr($0, 189, 7)}'

which give with your input

tr -d '\n' < se | awk '{printf "%s %s %s\n", substr($0, 8, 9),substr($0, 77, 6),substr($0, 189, 7)}'
marvelled answer blessed
  • have a look at printf, which by default do not end with a newline (contrary to print )

note also you can use subshell

( cmd1 arg 1
 cmd2 arg for 2
 cmd 3 ) > result

which will put output of cmds to result.

0

With bash

cat extract_words.sh

#!/bin/bash
concat=" "
min=$(($6+$7))
while read line
do
  concat="$concat$line"
  if test "${#concat}" -ge "$min" ; then
    break
  fi
done < "$1"
echo "${concat:$2:$3}" "${concat:$4:$5}" "${concat:$6:$7}"

You call it like that

./extract_words.sh "textExample.txt" 8 9 77 6 189 7

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .