1

I have two files with this structure:

File1.txt:

1,,
1,,
1,,
1,,
1,,
1,,
1,,
1,,
2,,
2,,
2,,
2,,
2,,
2,,
3,,
3,,
3,,
3,,
4,,
4,,
4,,
4,,
4,,
5,,
5,,
5,,
5,,

File2.txt:

0015
0016
0017
0018
0019

I want to replace File1.txt numbers with File2.txt in order, so it would look like this:

0015,,
0015,,
0015,,
0015,,
0015,,
0015,,
0015,,
0015,,
0016,,
0016,,
0016,,
0016,,
0016,,
0016,,
0017,,
0017,,
0017,,
0017,,
0018,,
0018,,
0018,,
0018,,
0018,,
0019,,
0019,,
0019,,
0019,,

And I haven't found an effective loop to do so, any help will be appreciated

Thank you.

2 Answers 2

3

Update:

With GNU sed and bash (process substitution):

sed -n -f <(sed -n "s|^\([^,]\)\(.*\)$|\1{h;s/[0-9]*/\&\2/p;g}|p" File1.txt) File2.txt > File_new.txt

See: man sed and info sed

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

2 Comments

That's great!! is there any way to keep the changes in File1.txt? (I ask this because File1 actually has many columns in csv format)
This runs a new sed process for every line of the input file. It would be more efficient to do it in shell, or even to start up perl.
0

Read the 2nd file into a linear array. Use it as a lookup table, index by the first field of the 1st file. With non-numeric keys, use an associative array instead.

readarray -t mapping < File2.txt  # -t strips trailing newlines

while IFS=  read -r l;
    do pref=${l%%,*};
    printf '%s%s\n' "${mapping[pref-1]}" ",${l#*,}"
done < File1.txt

Or "${l#[0-9]}", or whatever other way will be most future-proof for getting the part of the line you aren't replacing.

Or shopt -s extglob and "${l#+([0-9])}" (extglob version of the regex [0-9]+)

This is pure-bash, no external commands. If the input files are potentially large, the same algorithm in perl would probably be faster. bash isn't great for speed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.