replace particular line with user provided variable with sed - sed

My code is like below; and it is not working.
line_number=$1;
variable1=$2;
sed '\$line_number c\
\$variable1' file > tmp.out
If I write like below then its working. Can you please suggest how can I make above code running?
sed '1 c\
<replace>abc</replace>' file > tmp.out

Take the variables out of single quotes, and you don't need any backslash before the variables:
sed "$line_number"' c\
'"$variable1" file > tmp.out

You can try this,
#!/bin/bash
line_number=$1;
variable1=$2;
sed "$line_number c\
$variable1" file > tmp.out
If you want to use your variables values inside sed, then you have use " (double quotes).
Then, It will be interpreted by shell.

Related

Sed find and replace Cpp file(.C)

I am having issues of editing huge C++ file where I am using sed to convert List(something) to List<something> why I am doing this because our List has been converted to template.
Command I have written in small shell file is like this
sed -i '/List/s/(/</g' $1
sed -i '/List/s/)/>/g' $1
But this command is converting the whole line associated with List to angular braces like,
some_Fun(List(something)) to some_Fun<List<something>>
I don't want sed to change some_Fun<> , sed should keep some_Fun() and change only List() to List<>.
You can use this sed:
sed 's/\(List\)(\([^)]*\))/\1<\2>/g' file
(OR)
sed 's/List(\([^)]*\))/List<\1>/g' file

What does this sed line do?

I am not familiar with sed. Now I came accross the following line in a shell script:
sed 's|'`pwd`'||'
what does the line do? Does it replace || with something else?
it removes any string that equals to current-working-directory in the input. because pwd may itself contain / instead of /, | used in sed command as pattern-delimiter.

Add text at the end of each line

I'm on Linux command line and I have file with
127.0.0.1
128.0.0.0
121.121.33.111
I want
127.0.0.1:80
128.0.0.0:80
121.121.33.111:80
I remember my colleagues were using sed for that, but after reading sed manual still not clear how to do it on command line?
You could try using something like:
sed -n 's/$/:80/' ips.txt > new-ips.txt
Provided that your file format is just as you have described in your question.
The s/// substitution command matches (finds) the end of each line in your file (using the $ character) and then appends (replaces) the :80 to the end of each line. The ips.txt file is your input file... and new-ips.txt is your newly-created file (the final result of your changes.)
Also, if you have a list of IP numbers that happen to have port numbers attached already, (as noted by Vlad and as given by aragaer,) you could try using something like:
sed '/:[0-9]*$/ ! s/$/:80/' ips.txt > new-ips.txt
So, for example, if your input file looked something like this (note the :80):
127.0.0.1
128.0.0.0:80
121.121.33.111
The final result would look something like this:
127.0.0.1:80
128.0.0.0:80
121.121.33.111:80
Concise version of the sed command:
sed -i s/$/:80/ file.txt
Explanation:
sed stream editor
-i in-place (edit file in place)
s substitution command
/replacement_from_reg_exp/replacement_to_text/ statement
$ matches the end of line (replacement_from_reg_exp)
:80 text you want to add at the end of every line (replacement_to_text)
file.txt the file name
How can this be achieved without modifying the original file?
If you want to leave the original file unchanged and have the results in another file, then give up -i option and add the redirection (>) to another file:
sed s/$/:80/ file.txt > another_file.txt
sed 's/.*/&:80/' abcd.txt >abcde.txt
If you'd like to add text at the end of each line in-place (in the same file), you can use -i parameter, for example:
sed -i'.bak' 's/$/:80/' foo.txt
However -i option is non-standard Unix extension and may not be available on all operating systems.
So you can consider using ex (which is equivalent to vi -e/vim -e):
ex +"%s/$/:80/g" -cwq foo.txt
which will add :80 to each line, but sometimes it can append it to blank lines.
So better method is to check if the line actually contain any number, and then append it, for example:
ex +"g/[0-9]/s/$/:80/g" -cwq foo.txt
If the file has more complex format, consider using proper regex, instead of [0-9].
You can also achieve this using the backreference technique
sed -i.bak 's/\(.*\)/\1:80/' foo.txt
You can also use with awk like this
awk '{print $0":80"}' foo.txt > tmp && mv tmp foo.txt
Using a text editor, check for ^M (control-M, or carriage return) at the end of each line. You will need to remove them first, then append the additional text at the end of the line.
sed -i 's|^M||g' ips.txt
sed -i 's|$|:80|g' ips.txt
sed -i 's/$/,/g' foo.txt
I do this quite often to add a comma to the end of an output so I can just easily copy and paste it into a Python(or your fav lang) array

sed append text after match

I am writing a shell script to be able to append text after the match is found in a file
for example, in ~/.bash_profile file for the following line
PATH=$PATH:$HOME/bin
we need to append it with :/usr/java/jdk1.6.0_38/bin
so it'll become the following
PATH=$PATH:$HOME/bin:/usr/java/jdk1.6.0_38/bin
how could I do it with sed?
I tried with the following command from inside the console first, but it gave me error complaining 'sed: -e expression #1, char 13: unknown option to `s''
sed '/PATH/s/$/:/usr/java/jdk1.6.0_38/bin' ~/.bash_profile
what's wrong with my command above?
The problem is that you have regex delimiters in the replacement part of the substitute command. Either escape them with \ or use a different delimiter (comma in this case):
sed '/PATH/ s,$,:/usr/java/jdk1.6.0_38/bin,' ~/.bash_profile
This might work for you (GNU sed):
sed 's|PATH=$PATH:$HOME/bin|&:/usr/java/jdk1.6.0_38/bin|' ~/.bash_profile

replace text with a variable

I want to replace the value with a variable. The following does not work because of the single quote used in sed.
#!/bin/sh
set myvarM='pqr'
sed 's/:P03:M15/:P02:M1$myvarM/' mychange.txt > new_mychange.txt
Can I change the sed command or should I use something else?
#!/bin/sh
myvarM='pqr'
sed "s/:P03:M15/:P02:M1$myvarM/" mychange.txt > new_mychange.txt
Incidentally, to make the replacement in-place (ie not create a new file, but alter the original file), do this:
sed -i '' "s/:P03:M15/:P02:M1$myvarM/" mychange.txt
This says "use a blank as the increment suffix" - ie write out the same filename as the input
awk -v val="$myvarM" '{sub(/:P03:M15/, ":P02:M1" val); print}' filename
Note that in a bourne-type shell ("/bin/sh") the set command sets the positional parameters. Your first line sets $1 to the value myvarM='pqr' -- the myvarM variable continues to be unset.