my protoype sed -e 'stringStart; s/$/stringEnd/' -i ./tabs.txt
adds stringEnd behind each line as wanted
but ignores stringStart
its my first time using sed, so i read here:
https://www.linuxhowtos.org/System/sedoneliner.htm?print=246
https://stackoverflow.com/a/2869736/2891692
source tabs.txt
&clipboard
Pana
expected result tabs.txt
stringStart&clipboardstringEnd
stringStartPanastringEnd
result with this prototype tabs.txt
&clipboardstringEnd
PanastringEnd
Related
I want to replace '_v' with a whitespace and the last dot . into a dash "-". I tried using
sed 's/_v/ /' and tr '_v' ' '
Original Text
src-env-package_v1.0.1.18
output
src-en -package 1.0.1.18
Expected Output
src-env-package 1.0.1-18
This might work for you (GNU sed):
sed -E 's/(.*)_v(.*)\./\1 \2-/' file
Use the greed of the .* regexp to find the last occurrence of _v and likewise . and substitute a space for the former and a - for the latter.
If one of the conditions may occur but not necessarily both, use:
sed -E 's/(.*)_v/\1 /;s/(.*)\./\1-/' file
With your shown samples please try following sed code. Using sed's capability to store matched regex values into temp buffer(called capturing groups) here. Also using -E option here to enable ERE(extended regular expressions) for handling regex in better way.
Here is the Online demo for used regex.
sed -E 's/^(src-env-package)_v([0-9]+\..*)\.([0-9]+)$/\1 \2-\3/' Input_file
OR if its a variable value on which you want to run sed command then use following:
var="src-env-package_v1.0.1.18"
sed -E 's/^(src-env-package)_v([0-9]+\..*)\.([0-9]+)$/\1 \2-\3/' <<<"$var"
src-env-package 1.0.1-18
Bonus solution: Adding a perl one-liner solution here, using capturing groups concept(as explained above) in perl and getting the values as per requirement.
perl -pe 's/^(src-env-package)_v((?:[0-9]+\.){1,}[0-9]+)\.([0-9]+)$/\1 \2-\3/' Input_file
I have a list
>ANARCI-HMM_human_167.7|pdb|7EPU|A
>ANARCI-HMM_alpaca_173.7|pdb|7EVY|E
>ANARCI-HMM_alpaca_172.8|pdb|7F2O|S
>ANARCI-HMM_alpaca_171.8|pdb|7F4F|S
>ANARCI-HMM_alpaca_173.6|pdb|7F8W|D
I want to remove from ANARCI to the first vertical bar |.
expecting
>pdb|7EPU|A
>pdb|7EVY|E
>pdb|7F2O|S
>pdb|7F4F|S
>pdb|7F8W|D
I tried
sed 's/ANARCI.*\|//g'
but didn't work.
Do you have any idea how to sed in this case?
Using sed
$ sed 's/[A-Z][^|]*|//' input_file
>pdb|7EPU|A
>pdb|7EVY|E
>pdb|7F2O|S
>pdb|7F4F|S
>pdb|7F8W|D
If you want to remove from ANARCIat the first vertical bar |, try this:
sed 's/ANARCI[^|]*\|//g'
or
sed 's/ANARCI[^|]*\|(.*)/\1\2/'
1st solution: With your shown samples, please try following sed code.
sed -E 's/(.*)ANARCI[^|]*\|(.*)/\1\2/' Input_file
Explanation: Adding detailed explanation for above sed code.
Using -E option of sed to enable ERE(extended regular expression) for program.
Then using sed's capability of storing matched patterns into temporary buffer memory(called capturing groups), by which we can make use of caught values while substitution.
Creating 2 capturing groups here, 1st which has everything before ANARCI string and 2nd capturing group which has everything after first pipe(matching from ANARCI to till first pipe) to get rest of part after first pipe.
While performing substitution substituting line with 1st and 2nd capturing group.
2nd solution: You could use awk for this task also, use match function of awk. Simple explanation would be, using match function of awk and matching only part which you don't required in output, while printing the values printing everything else apart from matched part(which is not required).
awk 'match($0,/ANARCI[^|]*/){print substr($0,1,RSTART-1) substr($0,RSTART+RLENGTH+1)}' Input_file
3rd solution: Adding 1 more solution in awk, where setting field separators to: from string ANARCI to till first occurrence of pipe. Then in main awk program printing 1st and last field, required values as per shown samples.
awk -v FS="ANARCI[^\\\\|]*\\\\|" '{print $1 $NF}' Input_file
Try:
sed 's/ANARCI[^|]*\|//g'
to not match the |
I want to substring the File name in unix using sed command.
File name : Test_Test1_Test2_10082019_030013.csv.20191008-075740
I want the characters after the 3rd underscore or (all the characters after Test2 ) i need to be printed .
Can this be done using sed command?
I have tried this command
sed 's/^.*_\([^_]*\)$/\1/' <<< 'Test_Test1_Test2_10082019_030013.csv.20191008-075740'
but this is giving result as 030013.csv.20191008-075740
I need it from 10082019_030013.csv.20191008-075740
Thanks
Neha
To remove from the beginning up to including the 3rd underscore you can use
sed 's/^\([^_]*_\)\{3\}//' <<< 'Test_Test1_Test2_10082019_030013.csv.20191008-075740'
This removes the initial part that consists of 3 groups of (any number of non-underscore characters followed by an underscore). The result is
10082019_030013.csv.20191008-075740
If you use GNU sed you can switch it to extended regular expressions and omit the backslashes.
sed -r 's/^([^_]*_){3}//' <<< 'Test_Test1_Test2_10082019_030013.csv.20191008-075740'
Could you please try following.
sed 's/\([^_]*\)_\([^_]*\)_\([^_]*\)_\(.*\)/\4/' Input_file
Or as per Bodo's nice suggestion:
sed 's/[^_]*_[^_]*_[^_]_\(.*\)/\1/' Input_file
This might work for you (GNU sed):
sed 's/_/\n/3;s/.*\n//;t;s/Test2/\n/;s/.*\n//;t;d' file
Replace the third _ by a newline and then remove everything upto and including the first newline. If this succeeds, bail out and print the result. Otherwise, try the same method with Test2 and if this fails delete the entire line.
I have a file containing many blocks of lines. In each block, I have one numeric character of multiple digits (15353580 for instance). I need to extract all these numbers and put them as a column in a new file.
I came across this thread. The sed command does the job but does not separate the numbers from each other. Using the second example ("123 he23llo") of the most voted response, I would like to have 123-23 instead of 12323, where my '-' stands for a line break. How can I do so ?
You can use this sed,
sed -e 's/[^0-9]\+/-/g' -e 's/-$//'
Example:
$ echo "123 hel43lo 23fds" | sed -e 's/[^0-9]\+/-/g' -e 's/-$//'
123-43-23
I'm very much a junior when it comes to the sed command, and my Bruce Barnett guide sits right next to me, but one thing has been troubling me. With a file, can you filter it using sed to select only specific items? For example, in the following file:
alpha|november
bravo|october
charlie|papa
alpha|quebec
bravo|romeo
charlie|sahara
Would it be possible to set a command to return only the bravos, like:
bravo|october
bravo|romeo
With sed:
sed '/^bravo|/!d' filename
Alternatively, with grep (because it's sort of made for this stuff):
grep '^bravo|' filename
or with awk, which works nicely for tabular data,
awk -F '|' '$1 == "bravo"' filename
The first two use a regular expression, selecting those lines that match it. In ^bravo|, ^ matches the beginning of the line and bravo| the literal string bravo|, so this selects all lines that begin with bravo|.
The awk way splits the line across the field separator | and selects those lines whose first field is bravo.
You could also use a regex with awk:
awk '/^bravo|/' filename
...but I don't think this plays to awk's strengths in this case.
Another solution with sed:
sed -n '/^bravo|/p' filename
-n option => no printing by default.
If line begins with bravo|, print it (p)
2 way (at least) with sed
removing unwanted line
sed '/^bravo\|/ !d' YourFile
Printing only wanted lines
sed -n '/^bravo\|/ p' YourFile
if no other constraint or action occur, both are the same and a grep is better.
If there will be some action after, it could change the performance where a d cycle directly to the next line and a p will print then continue the following action.
Note the escape of pipe is needed for GNU sed, not on posix version