Copy Lines Containing Word from One File to Another File in Linux

Copy lines containing a specific text from one file to another file

You can just change your command in the following way and it will work:

awk '/Cmd:\[41\]/' file1.txt > file2.txt

Explanations:

'/Cmd:[41]/' will match lines that contain: Cmd:4 or Cmd:1 but will not match lines that contain literally Cmd:[41] as [...] are used in regex to define a character range, or a list of characters that can be matched therefore you need to escape them by adding a \ before them.

How do I copy lines next to line of another file in Linux?

Linux has paste command. Basic example is as follows.

paste file1 file2 > file3

How to find words from one file in another file?

You can use grep -f:

grep -Ff "first-file" "second-file"

OR else to match full words:

grep -w -Ff "first-file" "second-file"

UPDATE: As per the comments:

awk 'FNR==NR{a[$1]; next} ($1 in a){delete a[$1]; print $1}' file1 file2

Copy and paste content from one file to another file in vi

Since you already know how to cut/yank text, here are a few ideas for pasting it back into another file:

  • Edit the first file, yanking the text you want. Then open your second file from within vi (:e /path/to/other/file) and paste it
  • Open both files together in a split window and navigate between them using Ctrl + w, Up/Down either by:

    • vi -o /path/to/file1 /path/to/file2
    • From within the first file, Ctrl + w, s


Related Topics



Leave a reply



Submit