bash loop over two files and one file has two key values

Saeed

This is file1:

line1
line2
line3
line4
line5

This is file2:

value: line1
id: 12
--
value: line3
id: 20
--
value: line10
id: 22
--
value: line14
id: 8

I want to do this:

compare the two files and if the second column of value in file2 doesn't match the file1, then show the value line and id line

The expected output:

value: line10
id: 22
value: line14
id: 8

These are my failed attempts:

#!/usr/bin/bash

for all in "$(cat file1)"
do
    for ids in "$(cat file2 | grep value | awk {'print $2'})"
    do
        if [ "$all" != "$ids" ]
        then
            echo "$ids"
        fi
    done
done
Cyrus

With any awk. Elements of array f1 contain all rows of file1.

awk 'NR==FNR                     { f1[$0]=$0; next } 
     $1=="value:" && $NF!=f1[$NF]{ print; nr=NR }
     NR==nr+1                    { print } ' file1 file2

Output:

value: line10
id: 22
value: line14
id: 8

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

merge contents of two files into one file in bash

Knit one markdown file to two output files

Parse through two files and concatenate strings into one file using bash

Iterate over two dictionaries in one loop in python

Loop search and replace two-part string over file using PowerShell while preserving one of the parts

Comparing two values within a file using bash

How to map key and values from two files?

Compare two files and replace value in one of the file

Python - How do I update a dictionary with a loop that has one key and two values?

Is it possible to export one file with mysqldump command, with two tables , which one of them has foreign key?

Gather multiple columns into one key and two values

Loop over two lists

Bash loop using two input files as lists

Merge two-columns files into one file

Handle two XML files with one schema file

Combine two files of different lengths into one file

Bash loop with two values

Separate two columns from one file to two separate files in for loop

How to loop over a list containing two different pattern files in linux (bash)?

Python: How to create a loop that depending on values of a list sends sections of a separate file, to one of two output files?

Using two .cpp files and one header file

Combining two cpp files into one cpp file

Loop over two array values at a time

Concatenate two files in a loop to create new file?

How to compare entries in one file to two files?

How to output one value from a dictionary whose key has two values

Ansible - Combine two dictionaries, but with a loop over the one

merge contents of two files into one file by bash command

bash loop over two files and grep part of line then remove the line before if match