Remove/replace a dynamic String in file using unix - shell

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

Related

Remove word from a line - 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

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

Using sed to check make sure each line only has numbers and a comma?

I have a text file that looks like this:
0,16777215
16807368,16807368
621357328,621357328
621357403,621357403
1380962773,1380962773
1768589474,1768589474
Is there a way to use sed to make sure that each line ONLY has numbers and one comma? If a line is missing the comma, contains letters, is blank, has spaces, etc. - then I want to delete it.
With sed:
sed -nr '/^[0-9]+,[0-9]+$/p' File
To edit the file in-place:
sed -nri '/^[0-9]+,[0-9]+$/p' File
A portable solution:
sed -nE '/^[0-9]+,[0-9]+$/p' File
sed -e '/^[0-9]\+,[0-9]\+$/ !d' file
The address is a regular expression. If the line does not match the regular expression, the d (delete) command is applied. (The exclamation mark (!) inverts the condition.)

Replacing with sed wont work

I have a file called "washington", with capital spelled in 4 different
ways: Capital, capital, Capitol, capitol. Use the "sed" command
to replace all of them at once, with the correct spelling: capital.
I tried cat /washington | s '/[Cc]apit[ao]l/capital' but it wont work.
What do i do?
This will work:
$ cat /washington | sed 's/[Cc]apit[ao]l/capital/g'
Note that you need proper command in quotes. Starts with 's' for 'substitute' and ends with 'g' for 'global'. Global means replace all occurrences in the string.
sed 's/[Cc]apit[ao]l/capital/g' <filename>
If you want to change the file itself, i.e. write back to file
sed -i 's/[Cc]apit[ao]l/capital/g' <filename>
If you want to keep a backup (my suggestion) of the original file
sed -i.bak 's/[Cc]apit[ao]l/capital/g' <filename> will keep a backup named .bak
(See, i did not use cat anywhere)

Using sed to replace the first instance of an entire line beginning with string

I am attempting to write a bash script that will use sed to replace an entire line in a text file beginning with a given string, and I only want it to perform this replacement for the first match.
For example, in my text file I may have:
hair=brown
age=25
eyes=blue
age=35
weight=177
And I may want to simply replace the first occurrence of a line beginning with "age" with a different number without affecting the 2nd instance of age:
hair=brown
age=55
eyes=blue
age=35
weight=177
So far, I've come up with
sed -i "0,/^PATTERN/s/^PATTERN/PATTERN=XY/" test.txt
but this will only replace the string "age" itself rather than the entire line. I've been trying to throw a "\c" in there somewhere to change the entire line but nothing is working so far. Does anyone have any ideas as to how this can be resolved? Thanks.
Like #ruakh suggests, you can use
sed -i "0,/^PATTERN/ s/^PATTERN=.*$/PATTERN=XY/" test.txt
A shorter and less repetitive way of doing the same would be
sed -i '0,/^\(PATTERN=\).*/s//\1XY/' test.txt
which takes advantage of backreferences and the fact that not specifying a pattern in an s-expression will use the previously matched pattern.
0,...-ranges only work in GNU sed. An alternative might be to use shell redirect with sed:
{ sed '/^\(PATTERN\).*/!n; s//\1VAL;q'; cat ;} < file
or use awk:
awk '$1=="LABEL" && !n++ {$2="VALUE"}1' FS=\\= OFS=\\= file

Resources