I have a file test.txt that looks something like this:
something=1something-else=234another-something=5678
I would like to replace something-else=234 with something-else=***, for example, but the only information I have is the "match" that is something-else= and that there are exactly THREE characters after the equals sign. Currently I have this command that replaces everything on the line after the match:
sed -i -e 's/\(something-else=\).*/\1***/' test.txt
Result: something=1something-else=***
How can I adapt it to only replace three characters instead of the entire rest of the line?
You're looking for
sed -i -e 's/\(something-else=\).\{3\}/\1***/' test.txt
or, equivalently,
sed -i -e 's/\(something-else=\).../\1***/' test.txt
How can I adapt it to only replace three characters instead of the entire rest of the line?
You can use:
sed 's/\(something-else=\).../\1***/' file
something=1something-else=***another-something=5678
Here ... will match exactly 3 characters after something-else=.
You can also use a numbered in quantifier:
sed -E 's/(something-else=).{3}/\1***/' file
Related
I got this text in file.txt:
Osmun.Prez#mail.com:c7lB2m6b#3.a.a:tt_webid_v2=6990226111024612869; tt_webid=6990226111024612869; tt_csrf_token=VD5Nb_TQFH4RKhoJeSe2nzLB; R6kq3TV7=AHkh4PB6AQAA3LIS90nWf2ss0Q7ZTCQjUat4axctvhQY68DdUEz92RwpmVSX|1|0|e9d6917c2fe555827dcf5ee916ba9778079ab2a9; ttwid=1%7CAFodeNF0iZM2fyy-ZeiZ6HTpZoG_MSx6SmXHgGVQ-V4%7C1627538859%7C59ca1e4a56f9f537b55e655a6dabff88e44eb48502b164ed6b4199f5a5263cb0; passport_csrf_token_default=6f7653c3ce946a6ce5444723fb0c509b; passport_csrf_token=6f7653c3ce946a6ce5444723fb0c509b; sid_guard=0483b7d37f4e4bd20ab3046e29724798%7C1627538893%7C5184000%7CMon%2C+27-Sep-2021+06%3A08%3A13+GMT; uid_tt=27b52febe6222486b9f6b6a90ef4ffeace5ea25c09d29a1583be5a1ecf760996; uid_tt_ss=27b52febe6222486b9f6b6a90ef4ffeace5ea25c09d29a1583be5a1ecf760996; sid_tt=0483b7d37f4e4bd20ab3046e29724798; sessionid=0483b7d37f4e4bd20ab3046e29724798; sessionid_ss=0483b7d37f4e4bd20ab3046e29724798; store-idc=maliva; store-country-code=us; odin_tt=294845c8f7711db177f7c549a9f44edb1555031b27a2a485df809cd92c4e544ac0772bf462df5b7a100f6e488c45303cd62df3b6b950f0842520cd887850137b035d990f29cc8b752765e594560c977f; cmpl_token=AgQQAPNSF-RMpbE89z5HYF0_-2PcrxjXf4fZYP5_ZA
How can I delete everything from the string inside ( first & only instance ) from :tt_ to _ZA in file.txt keeping only Osmun.Prez#mail.com:c7lB2m6b#3.a.a using bash linux?
Thank you
Something like:
sed -i "s/:tt_.*//" file.txt
if you want to edit the file in place. If not, remove the -i switch.
The sed command means: replace (s), in each line of file.txt, all the chars (.*) starting by the pattern :tt_ with an empty string (//).
Or the command:
sed -i "s/:tt_.*_ZA//" file.txt
which is more adherent to what you ask for, but returns the same output.
Use pattern substitution:
i=$(cat file.txt)
echo "${i/:tt*_ZA}"
Assuming the general requirement is to remove everything after the 2nd : ...
Sample data:
$ cat file.txt
Osmun.Prez#mail.com:c7lB2m6b#3.a.a:tt_webid_v ... to end of line
some.one#home.com:B52_m6b#9_az.more.stuff:delete from here ... to end of line
One sed idea:
$ sed -En 's/^([^:]*:[^:]*).*$/\1/p' file.txt
Osmun.Prez#mail.com:c7lB2m6b#3.a.a
some.one#home.com:B52_m6b#9_az.more.stuff
Using awk
awk 'BEGIN{FS=OFS=":"}{print $1,$2}'
Using : as the delimiter, it is easy to extract the columns before :tt
This deletes all chars from ":tt_" to the last "_ZA", inclusive, in file.txt
Mac_3.2.57$cat file.txt | sed 's/\(\)[:]tt.*_ZA\(.*\)/\1\2/'
Osmun.Prez#mail.com:c7lB2m6b#3.a.a
Mac_3.2.57$
Or if it is always the first 2 values which are separated by colon (as per you example)
cat file.txt | cut -f1,2 -d’:’
I tried to remove the unwanted symbols
%H1256
*+E1111
*;E2311
+-'E3211
{E4511
DE4513
so I tried by using this command
sed 's/+E[0-9]/E/g
but it won't remove the blank spaces, and the digits need to be preserved.
expected:
H1256
E1111
E2311
E3211
E4511
E4513
EDIT
Special thanks to https://stackoverflow.com/users/3832970/wiktor-stribiżew my days have been saved by him
sed -n 's/.*\([A-Z][0-9]*\).*/\1/p' file or grep -oE '[A-Z][0-9]+' file
You may use either sed:
sed -n 's/.*\([[:upper:]][[:digit:]]*\).*/\1/p' file
or grep:
grep -oE '[[:upper:]][[:digit:]]+' file
See the online demo
Basically, the patterns match an uppercase letter ([[:upper:]]) followed with digits ([[:digit:]]* matches 0 or more digits in the POSIX BRE sed solution and [[:digit:]]+ matches 1+ digits in an POSIX ERE grep solution).
While sed solution will extract a single value (last one) from each line, grep will extract all values it finds from all lines.
This should do the job:
sed -E 's/^[^[:alnum:]]+//' file
Or if it is only the last 5 characters you need
sed -E 's/.*(.{5})$/\1/' file
I don't know much about sed, nor regex. I want to replace every line that contains only tabs by the string '0'. There are also lines in my file that contain only '\n'.
Basically I want to use the regular expression ^\h+$ and replace the matches with 0.
I tried:
sed -i 's/^\h+$/0/' file.txt
But it doesn't work
You can use:
sed -i.bak -E 's/^[[:blank:]]+$/0/' file
POSIX character class [[:blank:]] matches a space or tab which is same as \h in PCRE.
-i.bak is to keep original file in file.bak, in case you want to restore.
In sed the tabulator is called \t. One-or-more need a backslash \+:
sed -i -e 's/^\t\+$/0/' file.txt
I need to replace a particular range of characters in each line of a file.
I tried this
perl -i -pe 'r77,79c/XXX/g' file
I am trying to change the 77th to 79th characters to XXX using Perl, but above code is not working.
you want to replace chars at position [77-79] with XXX?
try
perl -i -piorig_* -e "substr($_,76,3)=XXX" file
a backup file called orig_file will be created cause of preventing possible dataloss..
perl -i -pe 's/.{76}\K.../XXX/' file
You wrote:
Actually i want to search a pattern in a file and whatever lines matching that pattern needs to be replaced to 50th & 51st character to XX
Using sed:
sed -r '/pattern/s/^(.{49})..(.*)$/\1XX\2/' file
sed "/pattern/ s/^\(.\{49\}\)../\1XX/" YourFile
we don't touch the end
I'm trying to uncomment file content using sed but with regex (for example: [0-9]{1,5})
# one two 12
# three four 34
# five six 56
The following is working:
sed -e 's/# one two 12/one two 12/g' /file
However, what I would like is to use regex pattern to replace all matches without entering numbers but keep the numbers in the result.
For complying sample question, simply
sed 's/^# //' file
will suffice, but if there is a need to remove the comment only on some lines containing a particular regex, then you could use conditionnal address:
sed '/regex/s/^# //' file
So every lines containing regex will be uncomented (if line begin with a #)
... where regex could be [0-9] as:
sed '/[0-9]/s/^# //' file
will remove # at begin of every lines containing a number, or
sed '/[0-9]/s/^# \?//' file
to make first space not needed: #one two 12, or even
sed '/[0-9]$/s/^# //' file
will remove # at begin of lines containing a number as last character. Then
sed '/12$/s/^# //' file
will remove # at begin of lines ended by 12. Or
sed '/\b\(two\|three\)\b/s/^# //' file
will remove # at begin of lines containing word two or three.
sed -e 's/^#\s*\(.*[0-9].*\)$/\1/g' filename
should do it.
If you only want those lines uncommented which contain numbers, you can use this:
sed -e 's/^#\s*\(.*[0-9]+.*\)/\1/g' file
Is the -i option for replacement in the respective file not necessary? I get to remove leading # by using the following:
sed -i "s/^# \(.*\)/\1/g" file
In order to uncomment only those commented lines that end on a sequence of at least one digit, I'd use it like this:
sed -i "s/^# \(.*[[:digit:]]\+$\)/\1/g" file
This solution requires commented lines to begin with one space character (right behind the #), but that should be easy to adjust if not applicable.
The following sed command will uncomment lines containing numbers:
sed 's/^#\s*\(.*[0-9]\+.*$\)/\1/g' file
I find it. thanks to all of you
echo "# one two 12" | grep "[0-9]" | sed 's/# //g'
or
cat file | grep "[0-9]" | sed 's/# //g'