Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to combine command diff with sed (or other commands) to solve this question?

FileRemote includes new lines that will be added to FileLocal in specific places.

FileRemote:
## Functions
Read Auto Hide List #101
Read Auto Move Win #102
# Read Yad Position #103
# Read Tiling Function #104 

## Styles
Read Fvwm Thumbnails #201
# Read Diary Thumbnails #202
Read MyBorder Style #203

## Menus
Read MyMenu #301
# Read Flat Menu #302

FileLocal:
## Functions
Read Auto Hide List #101
# Read Auto Move Win #102

## Styles
# Read Fvwm Thumbnails #201
Read Diary Thumbnails #202
# Read MyBorder Style #203

## Menus
Read MyMenu #301


1. Compare the two files. The main difference is the three lines (#103, #104, #302) in FileRemote.
2. Copy only the three lines to FileLocal respectively. For example, lines #103 and #104 will be added below line #102 in FileLocal. And #302 under #301.

As the result, to copy only the numeric tags from FileRemote that do not exist in FileLocal, and preserve the order.

RESULT (FileLocal):
## Functions
Read Auto Hide List #101
# Read Auto Move Win #102
# Read Yad Position #103
# Read Tiling Function #104 

## Styles
# Read Fvwm Thumbnails #201
Read Diary Thumbnails #202
# Read MyBorder Style #203

## Menus
Read MyMenu #301
# Read Flat Menu #302


What I have tried:

I tried diff command with different options but creates duplicate lines due to the difference with the 1st column "#". How to combine with sed or other commands? The line numbers (#101, #102, etc.) are unique in both files.

Thank you in advance.
Posted
Updated 21-Mar-22 10:51am

Maybe this article is a good starting point: LPTextFileDiff: another textfile compare utility.[^]

:)
 
Share this answer
 
Got the solution that works but is clumsy.

awk '{print $1 " " $(NF)}' FileLocal.txt > output.tmp
grep '^Read' output.tmp > output2.tmp

Output2.tmp (result):

Read #101
Read #202
Read #301
sed -i 's/Read/# Read/g' FileRemote.txt
sed -i 's/# # Read/# Read/g' FileRemote.txt

FileRemote.txt (result, all lines are tagged with "#")
Quote:
## Functions
# Read Auto Hide List #101
# Read Auto Move Win #102
# Read Yad Position #103
# Read Tiling Function #104

## Styles
# Read Fvwm Thumbnails #201
# Read Diary Thumbnails #202
# Read MyBorder Style #203

## Menus
# Read MyMenu #301
# Read Flat Menu #302

awk '                                       
NR==FNR {a[$2] = $1; next}
$(NF) in a {$1 = a[$(NF)]}
1' output2.tmp FileRemote.txt > Result.txt

$ sed -i 's/Read Read/Read/g' Result.txt

RESULT same as my opening question.

If there is a better code, I appreciate the help. The solution is for public usage.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900