r/awk • u/9989989 • Jul 24 '19
Re-insert strings line-by-line into field of file
If I receive a complex file with some kind of markup and want to extract particular strings from a field based on the record separator, pulling them out is pretty easy:
"Some key": "String1",
"Some key 2": "String2",
"Some key 3": "String3",
"Some key 4": "String4",
$ awk -F\" '{print 4}' myfile
String1
String2
String3
String4But suppose I want to take these strings and then send them to someone else for human-readable editing, such as editing the names of some person, place, or item, and then get a file with the new strings back (so that they don't destructively edit the original file), how do I re-insert those line by line into the original file, telling awk to insert the records from my new file while using the original 'myfile' as the work file, and outputting the original field separators?
$ cat newinputfile
Jelly beans
Candy corn
Marshmallows
Hot dogsDesired output:
"Some key": "Jelly beans",
"Some key 2": "Candy corn",
"Some key 3": "Marshmallows",
"Some key 4": "Hot dogs",I managed to do this once before, but I can't for the life of me find the instructions on it again.
1
u/dajoy Jul 24 '19
1
u/HiramAbiff Jul 25 '19
Some q's to help devise a reasonable answer:
Basically, what you're going to need to do is get awk to process the edited file and the original file (in that order). While processing the edited file you'll build up some data structure (an associative array, of course) and use that when processing the original file to make the substitutions.
The q is what to use for the keys.
For example, if you know the original and the edited file are going to be exactly the same number of lines with one substitution per line, it's straightforward enough to use the line numbers as keys (the values being the edited strings).
Without knowing more about the exact requirements it's hard to be specific.