1

I have the config file on local, whom I am appending some variables from different remote machines. The file content is as:

#!/bin/bash name=bob department=(Production) name=alice department=(R&D) name=maggie department=(Production R&D) 

The latest values updated in the file are the last one. So the expected output in the config file should be:

#!/bin/bash name=maggie department=(Production R&D) 

I want to remove the first two data of name and address except for the latest one which is last. But this should happen only if there are multiple same variables.

I referred and try this for my solution but not getting expected output: https://backreference.org/2011/11/17/remove-duplicates-but-keeping-only-the-last-occurrence/

8
  • 1
    So instead of appending, just overwrite the file. Commented Nov 30, 2020 at 9:53
  • Does this answer your question? How do I delete an exported environment variable? Commented Nov 30, 2020 at 9:54
  • 1
    It is not clear what you are looking for. Can you give a sample of what you see before the update and what you expect after? Commented Nov 30, 2020 at 10:15
  • 1
    department=(Production R&D) is a syntax error. You need some quotes. Commented Nov 30, 2020 at 10:23
  • 1
    is getting appended from the remote hosts where I cannot access the file to overwrite If you can append (you have write access to the file), you can overwrite (in 99.99% of the cases). Appending is done by redirecting the sshpass output the file Great! So instead of >> use >. Commented Nov 30, 2020 at 10:23

1 Answer 1

2

Would you please try the following:

tac file | awk '{ # print "file" reversing the line order: last line first line = $0 # backup the line sub(/#.*/, "") # remove comments (not sure if comment line exists) if (match($0, /([[:alnum:]_]+)=/)) { # look like an assignment to a variable varname = substr($0, RSTART, RLENGTH - 1) # extract the variable name (-1 to remove "=") if (! seen[varname]++) print line # print the line if the variable is seen irst time } else { # non-assignment line print line } }' | tac # reverse the lines again 

Output:

#!/bin/bash name=maggie department=(Production R&D) 

Please note the parser to extract variable names is a lousy one. You may need to tweak the code depending on the actual file.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the feedback. You cannot overwrite the file with a redirect. Please redirect to another filename, say file2 then rename it with mv -f file2 file. Good luck!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.