Replace a variable with the name of the file in Bash - bash

I have a scenario where I need to substitute a variable (the same variable) in multiple files within a directory with that name of the files.
For instance, I have a list of files with identical content as such;
file1
file2
file3
file4
The contents of each of these files is;
Some Text
Another line of text
I need to replace this $variable
What I would like to do is replace all instances of the pattern $variable with the name of said file.
i.e. the contents of file1 will become
Some Text
Another line of text
I need to replace this file1
and the contents of file2 will become
Some Text
Another line of text
I need to replace this file2
I have been looking into sed and think it's probably the way to go but I've often been wrong about scripting and it certainly isn't a strong point.

You can use this for loop:
while read -r f; do
sed -i.bak "s~\$variable~$f~" "$f"
done < <(grep -l '\$variable' file*)
Change glob pattern file* to * if you want to do this for each file in the directory.

Using GNU awk for inplace editing and word anchors:
awk -i inplace '{gsub(/\<\$variable\>/,FILENAME); print}' file*
The above will fail for file names containing & or \\<number>. If that's a problem the solution is to use match() or index() with substr() instead of a gsub().

Related

Copy all the text in "File1" and replace it with a specific text in File 2 using sed command

Let's say I have a file called File1.txt that has the string
Hamburger
And I have another file called File2.txt that has the string:
I love Pizza
I want to use the sed command to make changes such that it copies all the text from File1.txt i.e. Hamburger and replace it in File2.txt with the word Pizza so that the final output in File2.txt would be
I love Hamburger
Is there a way to do this suing the sed command ?
Here's an example of code I am trying to use but it doesn't work:
sed -e '/Hamburger/{r File1.txt' -e 'd}' File2.txt
You can try this sed
$ sed "s/Pizza/$(cat File1.txt)/" File2.txt
I love Hamburger
The question is tagged github-actions, so I am going to make some guesses.
You have a config file with some template data in it.
You want to automatically replace that with some real data that is stored in another file.
There are many ways you could do this, but here's one using envsubst
First, rewrite your template File2.txt this way
I love $Pizza
Then run this shell script:
export Pizza=$(<File1.txt)
envsubst '$Pizza' < File2.txt
This will print out the phrase you expect by expanding $Pizza within the file to the content of the corresponding environment variable, but not expanding any other things that look like environment variables.
This might work for you (GNU sed):
sed 's/.*/s#pizza#&#/' file2 | sed -f - file1
Turn file2 into a sed script and apply to file1.

replace different text in different lines using sed

I need to do the following:
I have two files, the first one contains only the lines that are going to be modified:
1
2
3
and the second contains the text that is going to be replaced in original file (final_output.txt)
13e
19f
16a
the original file is
wire1: 0x'd318
wire2: 0x'd415
wire3: 0x'd362
I want to get the following:
wire1: 0x13e
wire2: 0x19f
wire3: 0x16a
This is only a part of final_output.txt, because the file can contain at least 100 lines, and I pretend to do it using for, but I don't know how to implement it
awk to the rescue!
assuming the part after the single quote will be replaced.
$ awk -v q="'" 'NR==FNR {a[$1]=$2;next}
FNR in a {sub(q".*",a[FNR])}1' <(paste index rep) file
index is the index file, rep is the replacement file, and file is the original data file.
Another solution where file1 contains only the lines, file2 contains the text that is going to be replaced in original file and final_output.txt contains your original text.
for ((i=1;i<=$(wc -l < file1);i++)); do sed -i "$(sed -n "${i}p" file1)s#$(sed -n "$(sed -n "${i}p" file1)p" final_output.txt | grep -oP "'.*")#$(sed -n "${i}p" file2)#g" final_output.txt; done
Output
darby#Debian:~/Scrivania$ cat final_output.txt
wire1: 0x13e
wire2: 0x19f
wire3: 0x16a
darby#Debian:~/Scrivania$

How to grep file by another file's line using bash

The file1 store Sentence want to grep,Here 's content file1
.|||anymore .
,|||arguments
,|||atheists
,|||be
,|||because the
.|||because the
The problem is it contain space in some sentence , and the target file2 contain the space too, how can I search all file2 content to know whether file2 has these sentence which in file1 or not
If found it output grep result to anther file(append) ,If no found output a file to keep it
Try this-
while IFS= read -r i; do #Reading the file line by line and saving lines in $i.
grep "$i" file2 >> output #Grepping the line in the second file and outputting.
done < file1
Your question needs to be edited to give some more details though.

how to remove <Technology> and </Technology> words from a file using shell script?

My text file contains 100 lines and the text file surely contains Technology and /Technology words .In which ,I want to remove Technology and /Technology words present in the file using shell scripting.
sed -i.bak -e 's#/Technology##g' -e 's#Technology##g' my_text_file
This is delete the words and also make a backup of the original file just in case
sed -i -e 's#/Technology##g' -e 's#Technology##g' my_text_file
This will not make a backup but just modify the original file
You can try this one.
sed -r 's/<[\/]*technology>//g' a
Here is an awk
cat file
This Technology
More here
Mine /Technology must go
awk '{gsub(/\/*Technology/,"")}1' file
This
More here
Mine must go
By adding an extra space in the regex, it will not leave an extra space in the output.
awk '{gsub(/\/*Technology /,"")}1' file
This Technology
More here
Mine must go
To write back to original file
awk '{gsub(/\/*Technology /,"")}1' file > tmp && mv tmp file
If you have gnu awk 4.1+ you can do
awk -i '{gsub(/\/*Technology /,"")}1' file

Using a file's contents in a sed expression

I am looking to perform an operation with sed that appends to a specific part of a series of files like to:
sed -i "s/test:\n/&$(<test.file)/g" foo.txt
Is there a way I can take the output of a file or some BASH varaible and place it into sed for input into a file.
sed '/^test:$/{r test.file
d}' foo.txt
Should do the trick. Note that the literal newline after the filename is not necessary in all versions of sed, but is advisable. The r command reads the contents of the named file.

Resources