I need to replace the text from File2.txt to File1.txt between 5th and 8th position. It supposed to be any character between 5th and 8th position, I just need to replace with new characters in mentioned position and remaining position character should be same as it is.





Expected output:


Command I tried

sed '/substr($0,5,4)/r File2.txt' File1.txt >file_new.txt
  • Nice task to learn text processing in the shell. How did you try to solve it? What went wrong? – Philippos Jul 17 '17 at 8:18
  • i couldn't even able to get the exact command to test it. thanks @Philippos – Prakash Balusamy Jul 17 '17 at 8:20
  • Few of us get a command exactly right on the first try. What did you try? Also, is each file just one line long? – Scott Jul 17 '17 at 8:23
  • And if you edit your question after it has already been edited to get the formatting correctly, why don't you have a look how it has been done and keep the correct formatting? – Philippos Jul 17 '17 at 8:34
  • sed '/substr($0,5,4)/r File2.txt' File1.txt >file_new.txt is a command i tried @Scott – Prakash Balusamy Jul 17 '17 at 8:36

Probably easier to use combination of paste+cut

$ paste -d'\0' <(cut -c1-4 File1.txt) File2.txt <(cut -c9- File1.txt)
  • -d'\0' so that the inputs are combined without any character between
  • <() is process substitution
  • cut -c1-4 gives first four characters from each line
  • cut -c9- gives all characters from each line starting from 9th position

Another way is idiomatic two file processing using awk

$ awk 'NR==FNR{a[FNR]=$0; next} {print substr($0,1,4) a[FNR] substr($0,9)}' File2.txt File1.txt
  • NR==FNR{a[FNR]=$0; next} saves all lines from File2.txt in array a with line number as key
  • print substr($0,1,4) a[FNR] substr($0,9) use substr to extract necessary characters and insert the lines from File2.txt in between
  • Thanks @Sundeep, it works good. between one more doubt File1.txt: abcd9876efghijklmno1234 abcd9676efghijklmno2345 abcd9886efghijklmno3456 abcd9976efghijklmno4567 Expected Result: abcd1234efghijklmno abcd2345efghijklmno abcd3456efghijklmno abcd4567efghijklmno How to replace the 20-23rd position character to 5-8th position ? – Prakash Balusamy Jul 17 '17 at 14:24
  • should be easy to modify... awk '{print substr($0,1,4) substr($0,20,4) substr($0,9,11)}' File1.txt bit of playing with substr would have helped you... if an answer solves your question, see unix.stackexchange.com/help/someone-answers on what to do... – Sundeep Jul 17 '17 at 14:39
  • It's working perfectly :) – Prakash Balusamy Jul 18 '17 at 6:26

This looks somewhat bizarre, but:

dd if=file1 bs=1 count=4 > file3
dd if=file2 bs=1 count=4 >> file3
dd if=file1 bs=1 skip=8 >> file3  

You haven't mentioned copying over any newline in file2, and it looks as if you didn't want it (if there is one).

  • It's not working, I need to replace 10+ lines at a time in same position, always file1 & file 2 have same no of lines and i need to replace the values in file1 alone not in file2. – Prakash Balusamy Jul 17 '17 at 11:36
  • @PrakashBalusamy you are not helping yourself by not giving better input samples and expected output... – Sundeep Jul 17 '17 at 11:53
  • Edited the question @sundeep formatting doesn't looks good. Consider each word as skngle line in each file – Prakash Balusamy Jul 17 '17 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.