Problem : Cannot insert a text using sed
content of file
aa=
i want to add a text after aa= using sed?
the output should be like below
aa=testing
The following should do it:
sed 's/aa=/aa=testing/'
You can try awk if you like.
awk '/aa=/ {$0=$0"testing"}1' file
If you like to make sure it only replace line that only contains aa= and nothing more, do:
awk '/^aa=$/ {$0=$0"testing"}1' file
Related
I want to replace below line with next line in all the files. So what sed pattern is used for this. I have tried lot but not figured that out..
checkToken($token['token'])
checkToken($token)
This is what I have tried
sed -i -- 's/checkToken\(\$token\['token'\]\)/checkToken\(\$token\)/g' get_officers_v2.php
You just need to get your escape-characters (\) on the right place like:
sed -ie "s/\(checkToken(\$token\)\['token'\])/\1)/" get_officers_v2.php
I have file that looks like this :
1,2,3,4
5,6,7,8
I want to substitute 2rd column containing 6 to 89. The desired output is
1,2,3,4
5,89,7,8
But if I type
index=2
cat file | sed 's/[^,]*/89/'$index
I get
1,89,3,4
5,89,7,8
and if I type
index=2
cat file | sed 's/[^,]6/89/'$index
nothing changes.
Why is it like this? How can I fix this? Thank you.
Since you want to change the second column containing a 6 and you have a comma as field separator it is actually very easy with sed:
sed 's/^\([^,]*\),6,/\1,89,/`
Here we make use of back-referencing to remember the first column.
If you want to replace the 6 in the 5th column, you can do something like:
sed 's/^\(\([^,]*,\)\{4\}\)6,/\189,/'
It is, however, much more comfortable using awk:
awk 'BEGIN{FS=OFS=","}($2==6){$2=89}1'
I solved this by using awk
awk 'BEGIN{FS=OFS=","} {if ($2==6) $2=89}1' file >file1
I have a file that I need to append to certain lines.
I can get the line numbers and have been able to use sed to print the entry but not to append the entry.
All I need to do is something like
sed -n '$VAR s/$/,nosuid/' > to_file
Just can not get the syntax down.
Thank you.
Try doing this :
sed "$VAR s/$/,nosuid/" > to_file
Like Etan Reisner said in the comments, the quotes should be double quotes.
This might work for you:
sed -n $VAR's/$/,nosuid/' > to_file
I Have the file with the text strings for nucleotides (A, C, G, T). I would like to find specifics strings from a text file and delete them.
For example:
ACTGGGCTGTCCAACTG
ACTTCTGGGTCGAACTG
CCCACTTCTGGGTTCAA
And I would like to delete from all lines only this parts ACT and GGG
Then I will get the file with this strings:
CTGTCCAACTG
TCTTCGAACTG
CCCTCTTTCAA
sed can help you:
sed 's/ACT//g; s/GGG//g' inputFile
i.e. replace all occurrences of ACT and GGG with an empty string.
You can try:
awk '{gsub(/ACT|GGG/,"")}1' file
Using sed
sed -r 's/(ACT|GGG)//g' file
perl -pe 's/ACT|GGG//g' your_file
I am trying to sed strings in file. This is a file, sed_tmp, with one string:
, "127.0.0.2");
This is the sed command I use:
sed -r 's/[0-9{1,3}]\.[0-9{1,3}]\.[0-9{1,3]\.[0-9{1,3}]/XXX.XXX.XXX.XXX/g' ./sed_tmp
and the result is
, "12XXX.XXX.XXX.XXX");
but I need the result
, "XXX.XXX.XXX.XXX");
What am I doing wrong?
Write the {} outside of []
sed -r 's/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/XXX.XXX.XXX.XXX/g' ./sed_tmp
this works:
sed "s/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/XXX.XXX.XXX.XXX/g"
I've never seen the {1,3} syntax that you are using there, do you have a link to somewhere describing it?
Edit: Seems like sed uses it for slightly differently: http://www.grymoire.com/Unix/Sed.html#uh-35