0

I have 2 files: A.txt, B.txt

Both these files contain the letter NODE in them.

I want to replace NODE in each of the files with A_NODE and B_NODE in A.txt and B.txt, respectively.

I know there is a for loop and sed involved, but not able to figure this one out. Any suggestions?

I was thinking:

for file in *; do sed -i 's/NODE/$file.NODE/g' file; done
Cyrus
  • 84,225
  • 14
  • 89
  • 153
aram
  • 23
  • 3

2 Answers2

0
for file in *; do sed -i "s/NODE/$(basename -s .txt file)_NODE/g" file; done
  • The double quotes (") instead of single quotes (') enable bash variable interpolation (replacing the $ expression).
  • $(...) executes an arbitrary shell command and inserts the result.
  • basename -s .txt removes the directory part and the suffix .txt from the filename.
sapanoia
  • 789
  • 4
  • 14
  • That exact command did not work, but based of your comment, I modified a bit and this did: `for file in *; do sed -i "s/NODE/$(basename -s .txt "$file")_NODE/g" $file; done` – aram Jun 16 '17 at 23:52
0

Thank you @sapanoia:

This works:

for file in *; do sed -i "s/NODE/$(basename -s .txt "$file")_NODE/g" $file; done

aram
  • 23
  • 3