How to replace a string (characters and numbers) - linux

I would like to replace rs101 to rs102, however it shows unterminated "s" command with this.
sed -i 's/rs101/rs102' file.name.
Is there any other solution ?

You should insert / after rs102 .
sed -i 's/rs101/rs102/' file.name.
Syntax for Search and Replace:
sed 's/search/replace/' file.name

Related

Replacing strings with special characters in command line sed

I want to uncomment a line of a config file by replacing the line %% {some_string, []}, with {some_string, []} in the command line.
I have tried a few different formats using sed:
sed 's/%% {some_string, []},/{some_string, []}/' filename
sed "s/%% {some_string, []},/{some_string, []}/" filename
sed "s/'%% {some_string, []},'/'{some_string, []}'/" filename
sed 's/"%% {some_string, []},"/"{some_string, []}"/' filename
but every time have received the output sed: -e expression #1, char (some number): unterminated s' command
I am assuming it's something to do with my formatting because of my special characters, but can't seem to find any other posts with the same characters that could be causing the issue. I also cannot declare these strings as variables beforehand as I need this to run in the command line.
sed requires you to escape the square brackets.
sed 's/%% {some_string, \[\]},/{some_string, []}/' filename

Remove string using sed or awk, grep

I'm trying find and remove strings like:
[1126604244001,85.00], [1122204245002,85.00], [1221104246003,85.00],
[1222204247004,85.00], [1823304248005,85.00], [1424404249006,85.00],
85.00 = constans. I mean [xxxxxxxxxxxxx,85.00],
In notepad++ is simple:
find: "[^........].............,85.00]" and replace:""
I wolud like to use awk or sed to remove string automaticly without importing it to notepad++.
ok, I have file
temp.txt
[1126604244001,17.00], [1126604244001,17.00], [1126604244001,17.00],
[1126604244001,85.00], [1122204245002,85.00], [1221104246003,85.00],
[1222204247004,85.00], [1823304248005,85.00], [1424404249006,85.00], [1126604244001,17.00], [1126604244001,17.00],
My desire output
temp.txt
[1126604244001,17.00],[1126604244001,17.00],[1126604244001,17.00],[1126604244001,17.00],[1126604244001,17.00],
Thx in advance!
With sed, simply:
sed 's/\[[^]]*,85.00\],[[:space:]]*//g' filename
With this, everything that matches the regex \[[^]]*,85.00\],[[:space:]]* is removed. The regex matches [ followed by an arbitrary number of characters that are not ], followed by ,85.00], and optionally spaces; the only syntactically tricky bit is the [^]] character set which matches all characters other than ].
Alternatively with awk:
awk -v RS='],' -v ORS='],' '!/,85.00$/' filename
This splits the input into records delimited by ], and prints only those that don't end with ,85.00.
egrep -v '[^0-9]85\.00]' YourFile
remove (not empty) line with your pattern

How to find a match in file and then suffix that match

I have a file "test.xml"
That looks like the below:
CLASS="BAKERS"
GROUP="ABCYYYYY"
TRACK="DASD"
OWNERS="ALPHA"
GROUP="ABCXXXXX"
GROUP="ABCZZZZZ"
I want to use a single SED line command to find all occurrences of GROUP="ABC
Then within the "" I want add suffix: _DONE to all the matches found.
So the result should look like:
CLASS="BAKERS"
GROUP="ABCYYYYY_DONE"
TRACK="DASD"
OWNERS="ALPHA"
GROUP="ABCXXXXX_DONE"
GROUP="ABCZZZZZ_DONE"
This is the command I am using:
`sed -i.bkp '/^GROUP="ABC/ s/$/_DONE"/' test.xml`
but it is appending after the " and not within the ""
It's almost that. But $ means end of line so you have to substitute the last " using "$ instead:
sed -i.bkp '/^GROUP="ABC/ s/"$/_DONE"/' test.xml
you could also specify that there is some blank after the " with for instance "[ \t]*$
this may help:
sed -i 's/^GROUP="ABC[^"]*/&_DONE/' file
Try doing this but without the backticks :
sed -i.bak '/^GROUP="ABC/s/"$/_DONE"/' file
You can use this sed command
sed '/GROUP="ABC/s/\(.*\)"/\1_DONE"/'
Output :
CLASS="BAKERS"
GROUP="ABCYYYYY_DONE"
TRACK="DASD"
OWNERS="ALPHA"
GROUP="ABCXXXXX_DONE"
GROUP="ABCZZZZZ_DONE"
Use this:
sed -i.bkp 's/GROUP="ABC[A-Z]*/&_DONE/g' test.xml
I tested with your example and worked.

Sed: Replacing Date Format

I have a text file where there is a date string of "2014-06-01T03:11:00Z " in every line. I would like to replace that with "2014-06-01 03:11Z " using sed.
I've been trying to use this code but, it's failing me:
sed -i 's/[0-9]-[0-9]-[0-9]T[0-9]:[0-9]:[0-9]Z/[0-9]-[0-9]-[0-9] [0-9]:[0-9]Z/g' \
/home/aaron/grads/data/metars/${YMD}/latest.metars
Your digit sub-expressions only match a single digit, but the date contains 2 or 4 digits. A simple version that would match dates is:
sed -i 's/\([0-9]*-[0-9]*-[0-9]*\)T\([0-9]*:[0-9]*\):[0-9]*Z/\1 \2Z/g' \
/home/aaron/grads/data/metars/${YMD}/latest.metars
However, this matches zero or more digits at each position where digits are expected. You really want to insist on the correct number of digits in each segment. A more refined version is:
sed -i 's/\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)T\([0-9]\{2\}:[0-9]\{2\}\):[0-9]\{2\}Z/\1 \2Z/g' \
/home/aaron/grads/data/metars/${YMD}/latest.metars
And since your sed supports -i without specifying a back-up suffix (so it is probably GNU sed), you can probably abbreviate that to:
sed -r -i 's/([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}):[0-9]{2}Z/\1 \2Z/g' \
/home/aaron/grads/data/metars/${YMD}/latest.metars
Try this GNU sed command to replace all the lines which contains the date string with the string you mentioned,
sed -ri 's/^.*([0-9]{4})-([0-9]{2})-([0-9]{2})\w*([0-9]{2}):([0-9]{2}):([0-9]{2})(.)(.*)$/\1-\2-\3 \4:\5\7/g' file
Example:
$ cat aa
jgklj 2014-06-01T03:11:00Z jhgkjhvk
blaf 2015-12-08T03:15:02Z bvcjghj
$ sed -r 's/^.*([0-9]{4})-([0-9]{2})-([0-9]{2})\w*([0-9]{2}):([0-9]{2}):([0-9]{2})(.)(.*)$/\1-\2-\3 \4:\5\7/g' aa
2014-06-01 03:11Z
2015-12-08 03:15Z
For to replace date only and print all the other text as it is then run the below command.
sed -ri 's/^(.*)([0-9]{4})-([0-9]{2})-([0-9]{2})\w*([0-9]{2}):([0-9]{2}):([0-9]{2})(.)(.*)$/\1\2-\3-\5 \5:\6\8\9/g' file
Example:
$ cat aa
jgklj 2014-06-01T03:11:00Z jhgkjhvk
blaf 2015-12-08T03:15:02Z bvcjghj
$ sed -r 's/^(.*)([0-9]{4})-([0-9]{2})-([0-9]{2})\w*([0-9]{2}):([0-9]{2}):([0-9]{2})(.)(.*)$/\1\2-\3-\5 \5:\6\8\9/g' aa
jgklj 2014-06-03 03:11Z jhgkjhvk
blaf 2015-12-03 03:15Z bvcjghj
You can use this method also
$-sed -r 's/^([^T]+).((.*):){1,2}.([^Z])/\1 \3/g'

how to edit a line using sed or awk in linux containing a certain number or string

My Stress.k file is as follows
180.4430
*INCLUDE
$# filename
*STRESS_INITIALIZATION
*END
I want it to be like
180.4430
*INCLUDE
$# filename
*STRESS_INITIALIZATION
*/home/hassan/534.k
*END
for that I used sed as follows
a="$(cat flow.k)"
sed -i -e '/*END/i \*/home/hassan/$a.k ' Stress.k
where flow.k has only a single number like 534.k or something . Here sed put the line before END but it doesn't take the value of a , instead it puts the same alphabet and it doesn't understand $a.k.
Please also tell me how to delete the second last line or the line with a string hassan for example so that I can delete it first and the for the next step I use it to enter my required line.
if possible please also suggest the alternatives.
best regards
bash variables are only replaced when in double quotes, e.g.
sed -i -e "/*END/i \*/home/hassan/$a.k " Stress.k
Use double quotes to allow the variable to be expanded.
sed -i -e "/*END/i \*/home/hassan/$a.k " Stress.k
To replace the string, do it as you read in the file:
a=$(sed 's/534/100/' flow.k)
To delete a line:
sed '/hassan/d' inputfile
To read a file into the stream after the current line:
sed '/foo/r filename' inputfile

Resources