2

I want to append file text after last word of another file on a UNIX system.

File 1

& program executed successfully

File 2

Hello
World
This is a sample output 

I want to copy the first two words and paste them after the last word of file 2.

Expected output:

File3

Hello
World
This is a sample output & program

File2 has a newline character at the end, so when I tried using cat, it prints on a new line instead of printing after the last word on the same line. Please suggest a solution using UNIX commands and the bash shell.

7
  • I think you mean you want to copy the first two words, not the first two characters (letters), right?
    – terdon
    Feb 18, 2022 at 15:41
  • Right.. First two words after output keyword
    – Claksh
    Feb 18, 2022 at 15:43
  • Executed successfully I don't want to append.. And also there is a space at the end of file 2 after output
    – Claksh
    Feb 18, 2022 at 15:44
  • OK, what operating system are you using?
    – terdon
    Feb 18, 2022 at 15:46
  • Unix Operating system
    – Claksh
    Feb 18, 2022 at 15:47

6 Answers 6

3

You can select the first 2 words of every line of file2 with:

$ cut -d' ' -f1-2 file1 
& program

So you can then use that to do something like this:

$ printf '%s %s\n' "$(cat file2)" "$(cut -d' ' -f1-2 file1)" > file3
$ cat file3
Hello
World
This is a sample output & program

If your file can have more than one line, and you just want the first one, pass it through head:

printf '%s %s\n' "$(cat file2)" "$(cut -d' ' -f1-2 file1 | head -n1)"
3

I think that terdon's answer is good if you know that the second file is short. If you don't know that it is short, or you know that it is a long enough file to want to avoid reading it into a command substitution, then you may choose to avoid expanding more than the last line of the file in a substitution like that:

{
    sed '$d' file2
    printf '%s %s\n' "$( tail -n 1 file2 )" "$( cut -d ' ' -f -2 file1 )"
} >file3

This first uses sed to output all but the last line from file2, and then we use a modified variant of terdon's code to output the last line of file2 together with the first two space-delimited words from file1.

2

You could use a little ed program:

ed -s file2 << \EOF
$r !cut -d " " -f1,2 file1
-1,.j
w file3
EOF

Or, as a one-liner:

printf '%s\n' '$r !cut -d " " -f1,2 file1' '-1,.j' 'w file3' | ed -s file2
1
  • This is the first time I see ed in an answer.
    – Džuris
    Feb 19, 2022 at 15:14
1

Just a funky alternative with head, tail, cut and paste:

{ head -n -1 file2; ( tail -n 1 file2; cut -d ' ' -f -2 file1) | paste -sd ' ' ;} > file3

Or if head doesn't have the -1 alternative on your system, it can be replaced with sed '$d' file2

2
  • Note that not all implementations of head support -1 as an option-argument to -n.
    – Kusalananda
    Feb 18, 2022 at 17:34
  • Correct! I added an alternative. :) Feb 18, 2022 at 20:28
1

With awk:

awk 'NR==FNR { append=$1 OFS $2; next }
prev!=""{ print prev } { prev=$0 }
END{ print prev, append }' file1 file2

with saving current line into a temporary variable prev we delay outputting the lines by one so at the END when we processed all the lines, we append a text from file1 to the last line of the file2.

0
#!/usr/bin/python
f1=open('file1','r')
ext=" ".join(f1.readline().strip().split(' ')[0:2])
f2=open('file2','r')
k=f2.readlines()
k[-1]="{0} {1}".format(k[-1].strip(),ext)
for i in k:
    print i.strip()



output

Hello
World
This is a sample output & program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.