Remove word from a line - Bash - bash

I want to remove the & from all the line
?daypartId=1&catId=1
?daypartId=1&catId=2
?daypartId=1&catId=11
?daypartId=1&catId=10
?daypartId=1&catId=6
?daypartId=1&catId=4
?daypartId=1&catId=14
?daypartId=1&catId=5
?daypartId=1&catId=3
?daypartId=1&catId=8
Expected output:
?daypartId=1&catId=1
?daypartId=1&catId=2
?daypartId=1&catId=11
?daypartId=1&catId=10
?daypartId=1&catId=6
?daypartId=1&catId=4
?daypartId=1&catId=14
?daypartId=1&catId=5
?daypartId=1&catId=3
?daypartId=1&catId=8
removing & from the input is what i need. I am stuck at this problem please help.

You could do simply with sed like below:
sed 's/amp;//' myfile.txt
This would search for amp; and replace it with an empty string in a file called myfile.txt
If you want to replace it within the file, then you could use -i option as below:
sed -i 's/amp;//' myfile.txt
If you have multiple such occurrences in a line, you could use a global replacement as below:
sed 's/amp;//g' myfile.txt

Related

Search for a pattern and replace it in shell script

Hello I have a file with multiple lines. I need to search for a pattern ("") and replace it with another one (/"") with condition that its suffix with any string.
Ex:
cat myfile
27;"";"firstName"";"lastName";"user1";6;"Change!1"
28;"aaa#g.com";"Pravej0001";"Khan001";"test_u_001";7;"Change!1"
I need to replace firstName"" with firstName/"" in line number one.
I have already tried below code in sed :
sed 's/[A-Za-z0-9]""/\/""/g' myfile
but it is giving output as
27;"";"firstNam/"";"lastName";"user1";6;"Change!1"
28;"aaa#g.com";"Pravej0001";"Khan001";"test_u_001";7;"Change!1"
where e is replaced with / . I dont want to disturb the firstName.
Expected output should be like this:
27;"";"firstName/"";"lastName";"user1";6;"Change!1"
28;"aaa#g.com";"Pravej0001";"Khan001";"test_u_001";7;"Change!1"
Any help in sed/awk/shell will work.
sed 's/\([A-Za-z0-9]\)""/\1\/""/g' myfile
or more robustly across locales:
sed 's/\([[:alnum:]]\)""/\1\/""/g' myfile

Append/replace string to a line using sed

I am trying to parse a file test.txt and append "https://" to the following string within the file:
"1234.def.efj.abcd-1.nopqrs.com"
Note: the string has white space and the quotes at the beginning and end. Also def, efj, and nopqrs.com are static and won't change.
I am trying to accomplish this via sed. So far I have:
sed -e 's#^\s*[\"][0-9]+[\.]def[\.]efj[\.][a-z]+[\-][a-z]+[\-][0-9][\.]nopqrs[\.]com$#\s+[\"]https[\:][\/][\/][0-9]+[\.]def[\.]efj[\.][a-z]+[\-][a-z]+[\-][0-9][\.]nopqrs[\.]com#g' test.txt
When I run that command I just get the file output without the change.
I have gone through other sed posts and can't seem to figure out what is wrong with my expression. Thanks in advance for your help!
Something like this should work :
sed -i 's#"\([^"]*\.nopqrs\.com\)"#"https://\1"#g' file.txt
Explanation :
sed -i : inplace editing. sed will update file.txt accordingly. Note that the behaviour and syntax is implementation specific.
"\([^"]*\.nopqrs\.com\)" :
[^"]*\.nopqrs\.com: we look for any number of characters that are not quotes, followed by .nopqrs.com
\( ... \) : syntax for sed to create a capture group with the content matched by the expression between the parenthesis
\1 : we display the content of the first capture group

Remove/replace a dynamic String in file using unix

I have File containing the data like below
File Name :- Test.txt
TimeStamp_2017-12-43 09:09:14.0999/-ext-10100/Year/Month/Day
TimeStamp_2000-12-43 07:09:14.0999/-ext-10200/Year/Month/Day
TimeStamp_2015-12-43 06:09:14.0999/-ext-10200/Year/Month/Day
TimeStamp_2010-12-43 05:09:14.0999/-ext-10200/Year/Month/Day
TimeStamp_2011-12-43 04:09:14.0999/-ext-1090/Year/Month/Day
TimeStamp_2018-12-43 03:09:14.0999/-ext-920/Year/Month/Day
TimeStamp_2013-12-43 02:09:14.0999/-ext-1200/Year/Month/Day
TimeStamp_2016-12-43 01:09:14.0999/-ext-02/Year/Month/Day
Here i need to replace or remove below format in each line
TimeStamp_*/-ext-*
**Input line in file(Sampel TimeStamp value and -ext- value is changing every time)
TimeStamp_2017-12-43 09:09:14.0999/-ext-10100/Year/Month/Day
Ouput Line after remove or replace
Year/Month/Day
Can any one help on this Question
Simply with **sed**:
sed 's#.*-ext-[^/]*/##' file
Use below sed command, it will work for you. How will it work? First it will find the pattern TimeStamp_.*-ext-.* (here you need to add dot . with * to inform sed command that you are using * as wild card character) and replace with a blank line and second expression /^\s*$/d will search for blank line and remove it and finally you will get your required output. Every expression is separated with ; in sed command.
sed -e 's/TimeStamp_.*-ext-.*//;/^\s*$/d' Test.txt > tmp.txt
mv tmp.txt Test.txt
Hope this will help you.
When you wat to keep everything after the second slash, use
cut -d"/" -f3- Test.txt

SED change second occurence in all lines

The normal expression to change server1 to server1-bck is
sed -i 's/server1/server1-bck/g' file.out
so all server1 will be changed to server1-bck. What I need is to change the second occurence of the expression in every line.
For example,
before text:
rename files tsm_node1 //server1/document/users/ //server1/document/users/
desired after text:
rename files tsm_node1 //server1/document/users/ //server1-bck/document/users/
How can I do that?
echo "rename files tsm_node1 //server1/document/users/ //server1/document/users/" |\
sed 's/server1/server1-bck/2g'
sed's famous substitution works like this:
sed 's/regex/replacement/flags'
Flags could be a number, in your case 2 for advising sed to execute this command on 2nd occurrence and if you need more, therefore the g flag is. If you are sure, there are no more items to be substituted, you can leave and forget the g flag.
If you don't pipe and have a file, do this:
sed -i 's/server1/server1-bck/2g' file.out.
Additionally you can replace parts of your regex pattern with sed's & replacement if you want to substitute with that what you have found and will have:
sed -i 's/server1/&-bck/2g' file.out.

Replacing/removing square brackets in a string

I have the following text in a file:
Names of students
[Name:Anna]
[Name:Bob]
[Name:Carla]
[Name:Daniel]
[ThisShouldNotBeBeRemoved]
End of all names
Blablabla
I want to remove all lines of the text file where there is an occurrence of the string in the format of [Name:xxx], xxx being a name as a string of any length and consisting of any characters.
I have tried the following, but it wasn't successful:
$ sed '/\[Name:*\]/d' file > new-file
Is there any other way I could approach this?
I would use grep with -v
-v, --invert-match
Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)
grep -v "\[Name:"
You need to use .* not just * ...
sed '/\[Name:.*\]/d' file > new-file
* on it's own is meaningless in this particular circumstance. Adding . before it signifies "match any character zero or more times" — which I think is what you're wanting to do.
If you wanted to do an in-place edit to the original file without re-directing to a new one:
Linux:
sed -i '/\[Name:.*\]/d' file
macOS:
sed -i '' '/\[Name:.*\]/d' file
* note - this overwrites the original file.
You missed out something,
sed '/\[Name:.*\]/d' file > new-file
This would remove your lines that match.
.* This matches any character zero or more than once.
sed '/\[Name:[[:alpha:]]+\]/d' file
Names of students
[ThisShouldNotBeBeRemoved]
End of all names
Blablabla
OR if you don't want to create new file then try this,
sed -i '/[Name:.*]/d' file

Resources