How to use --file=script-file option to input a file to search and replace in sed command - linux

I am doing a jenkins migration using jenkins-cli where in one step I am using sed command to replacing values manually as like below :
sed 's/mukesh/architect/g' target_file
But I would like to enter all the possible values in Input file with two column with delimeter as = and supply to target file
Input file looks like
ex:
mukesh=architect
abdul=manager
Now I want to use this file as input in sed command for search and replace in my target file. Instead of using s///g manually, I want to use the below option that I found in man page
-f script-file, --file=script-file
But not sure how to use this input file to auto search and replace the pattern in to the target file. It would be grateful if I get any samples, examples.

You can use below code to read input file, parse it and update outfile.
Here I am reading input file, separating values based on delimeter "=" and then updating outfile/target file.
while read name
do
x=`echo $name|cut -d"=" -f1`
y=`echo $name|cut -d"=" -f2`
sed -i "s/$x/$y/g" outfile
done < inputfile
This should solve your problem. Let me know if you are looking for something else or extra.cheers :)

Related

append a user specified suffix to the output of cat command

I would like to append a user specified suffix to the end of a file based on certain condition specified beforehand. I have the filenames stored in a file called changedfile.txt . I am executing the following command to get filename without any extension code.
cat changedfile.txt | cut -d "." -f1
I want to add a user provided suffix before the extension.
For example : If the output of the previous command was a/b/c.toml, and the user provided suffix is _backup, I want my final file to be renamed from a/b/c.toml to a/b/c_backup.toml. I have a for loop to handle the changing user suffix. I need a way to append the suffix to the file.
I thought something like this would work( thought += appends strings).
cat changedfile.txt | cut -d "." -f1 +backup
or
cat changedfile.txt | cut -d "." -f1 +=backup
got this error (cut: +backup: No such file or directory). I can understand why that command doesn't work.
Would appreciate if someone can get this working. For now even if there was a way to get it working for one suffix it's fine.I am using bash 3.2 .
cut just outputs the selected field, it can't make changes. You would need to pipe to some other tool to append something.
But you don't want to append, you want to insert in the middle of the line. You can do that entirely with sed.
sed 's/\./_backup./' changedfile.txt

How can replace a specific line in a text file with a shell script?

I am trying to replace a specific line in a txt file with my shell script, for example;
cat aa.txt:
auditd=0
bladeServerSlot=0
When I run my script I would like to change "bladeServerSlot" to 12 as following;
cat aa.txt:
auditd=0
bladeServerSlot=12
Could you please help me?
Using sed and backreferencing:
sed -r '/bladeServerSlot/ s/(^.*)(=.*)/\1=12/g' inputfile
Using awk , this will search for the line which contains bladeServerSlot and replace the second column of that line.
awk 'BEGIN{FS=OFS="="}/bladeServerSlot/{$2=12}1' inputfile
perl -pe 's/bladeServerSlot=\K\d+/12/' aa.txt > output.txt
The \K is a particular form of the positive lookbehind, which discards all previous matches. So we need to replace only what follows. The s/ is applied by default to $_, which contains the current line. The -p prints $_ for every line, so all other lines are copied. We redirect output to a file.
Is it really necessary to replace the line in your example? As bladeServerSlot is a variable you could reset the value.
bladeServerSlot=`any command`
Or you could just let this variable be filled by a Parameter provided to this script.
bladeServerSlot=$1
With $1being the first parameter of your script. I think this would be the cleaner way do solve your issue than to do fancy regex here. The sed/perl solutions will work, but they are not very clear to other people reading your script.

Search and delete in ubuntu

Hi i am using ubuntu shell, i am trying to delete unique line in a file after the user has key in 2 inputs however is not deleting in the txt docs. Is there something wrong with my code? thanks. ":" is a delimiter in the txt
read input1
read input2
sed "/$input1:$input2/d" sample.txt
You need inplace change and you could use -i option with your command like:
sed -i.bak "/$input1:$input2/d" sample.txt
So post your command you will get additional file:
sample.txt.bak - as original file
sapmple.txt with line containing your pattern deleted.

grep from a input file, multiple lines while the input file has ^name

I would really appreciate some help with this:
I have a huge file, I will give you an example of how it is formatted:
name:lastname:email
I have a input file with lots of names set out like this example:
edward
michael
jenny
I want to match to name column from the huge file to the name in the input file, and only if it is an exact match (case insensitive)
Once it finds a match I want it to output a .txt with all of the matchs
I think I can use a command something like ^Michael: to give it.
Can anyone help me with this grep problem?
sorry if I am not too clear its very late and I have been on this problem for ages
"Centos 5, "grep -i -E -f file.txt /root/dir2search >out.txt"
file.txt containing
^michael:
^bobert:
^billy:
Doesn't find anything.
grep -i -E -f inputfile namesfile > outputfile will do what you want, if your input file consists of one input name per line, in the pattern you already suggested:
^Michael:
^Jane:
^Tom:
-i: case-insensitive matching
-E: regexp pattern matching (often the default, but I don't know how your environment is set up)
-f: read patterns from a file, one pattern per line
>: redirect the output to a file
To get the existing input file you described (space-separated names) into the new format, you could use:
sed -r 's/([^ ]+)[ $]?/^\1:\n/g;s/\n$//g' inputfile > newinputfile

Grep: Copy a link with specific text

I have a text file with many links which aren't in separate lines.
I want to save in another file probably, all the links which contains a specific word.
How can I do this with grep?
EDIT:
To become more specifique, I have a messy txt file with many links. I want to copy in onother file all links starting with https:://, ending with .jpg and contains anywhere "10x10" string for example
You can get all the lines containing a specific word from the file like this:
LINKS=$(cat myfile.txt | grep MYWORD)
Then with LINKS, you can use a delimiter to create an array of links, which you can print to another file.
# Using a space as the delimeter
while IFS=' 'read -ra ind_link
do
echo $ind_link >> mynewfile.txt
done <<< "$LINKS"
Something along those lines I think is what you are looking for no?
Also if you need to refine your search, you can use the grep options such as -w to get more specific.
Hope it helps.
Could you give us the specific word and an example of input file ?
You could try to use egrep or/and sed like this (for example) :
egrep -o "href=\".*\.html\"" file|sed "s/\"\([^\"]*\)/\1/g"
Another exemple for all kind of http/https ressources links (whithout spaces in the URL) :
$ echo "<a href=http://titi/toto.jpg >"|egrep -o "https?:\/\/[^\ ]*"
http://titi/toto.jpg
$ echo "<a href=https://titi/toto.htm >"|egrep -o "https?:\/\/[^\ ]*"
https://titi/toto.htm
You have to customize the regexp according to your needs.

Resources