ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a rather long file with ids (integer) in it, under the circumstances if I do not do some processing they appear on comma separated lists
about 10 per line, for example:
1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010
now I want to replace these ids one by one with new ones and I can allocate
the new ids into a separate list, for example:
1001, 2001
1002, 2002
etc.
now the numbers are not running numbers and not in some order or as in these examples.
The question is how can I perform the replacement in file 1 taking the new ids from file 2 one by one. There is a chance that an id may occur more in the file 1, but it is highly unlikely so it could be limited to replacement of first occurence.
How should I go on? can I pipe it through sed somehow or should I make a short program? I can still modify the format of file 2 to for example:
/1001/,/2001/
this I could maybe pipe into sed as a variable, but at this moment my brain fails me - so help would be appreciated.
If the file format is not too sensitive, you could get by with a pretty simple awk script.
Code:
#!/usr/bin/awk -f
BEGIN {
# Accept any newline convention; ignore leading and trailing whitespace.
RS = "[\t\v\f ]*(\r\n|\n\r|\r|\n)[\t\v\f ]*"
# Fields are separated by commas, with optional whitespace.
FS = "[\t\v\f ]*,[\t\v\f ]*"
# For output, use newlines and commas only.
ORS = "\n"
OFS = ","
# First file specifies replacements.
file = 0
}
# Increase file number when the first record is seen.
(FNR == 1) { file++ }
# Record replacements only from the first file.
(file == 1 && NF >= 2) { replace[$1] = $2 }
# All other files:
(file > 1 && NF > 0) {
for (i = 1; i <= NF; i++) {
value = $i
if (value in replace)
value = replace[value]
if (i < NF)
printf("%s%s", value, OFS)
else
printf("%s%s", value, ORS)
}
}
You specify two or more input files to the script. The first one contains the replacements: old values in the first column, and replacement values in the second column.
The rest of the input files will be split at commas (with whitespace around a comma removed). The values are only replaced if the entire field matches. On output, it uses commas (OFS) between fields, and newline (ORS) to end each line.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.