Is it possible to use Sed to locate between two IP Addresses?
Currently am doing:
sed '/85.159.56/s/$/ --- API SYSTEMS/'
But i have a specific range i want to have print out next to it
(This is part of a bigger script)
For example:
sed '/192.200.160.0 - 192.200.191.255/s/$/ --- APIv2 SYSTEMS/'
I know this is not the correct format.
But ideally i want Sed to locate the string between these two ranges
Then print out next to it --- APIv2 SYSTEMS/
I have tried many ways and unable to accomplish, Making me think Sed not the tool for the job.
Related
I have a csv file and in a number of fields there are floating point numbers where the negative at the end of the number, I want these to be altered so as to have the minus/negative symbol at the front.
ie
23.4954-,23.12-
0.23-,16.5453
2495.1-,12,134-
I would like those to read
-23.4954,-23.12
-0.23,16.5453
-2495.1,-12,134
Out of 20 columns and a few thousand rows, there are probably about 80 instances per file, but it is a real pain to just go and replace them
I was hoping to use sed on the files to alter them if possible.
Any help is really appreciated.
Use the following approach:
sed -ri 's/([0-9.]+)-/-\1/g' filepath
-r (--regexp-extended) - allows using extended regular expressions rather than basic regular expressions
-i - option which allows to change file in place
You could use this sed command:
sed -E 's/([0-9.]+)-/-\1/g' file
This moves the - sign before a number composed of 0-9. characters.
I'm trying to change the timecode found from one format into another, basically to remove the milliseconds off the end of a file and update it. This is to remove extra milliseconds from a transcription timecode software and make it look pretty for file for client.
Input looks like this:
00:50:34.00>INTERVIEWER
Why was it ............... script?
00:50:35.13>JOHN DOE
Because of the quality.
So I'm trying to use grep to match the timecode and got it working with following expression.
grep [0-9][0-9][:][0-9][0-9][:][0-9][0-9]\.[0-9][0-9] -P -o transcriptionFile.txt
Output looks like this:
00:50:34.00
00:50:35.13
So now I'm trying to take timecode and update the file with updated values like:
00:50:34
00:50:35
How do I do that? Should I use a pipe to push it over to sed so I can update the values in the file?
I've also tried to use sed with following command:
sed 's/[0-9][0-9][:][0-9][0-9][:][0-9][0-9]\.[0-9][0-9]/[0-9][0-9][:][0-9][0-9][:][0-9][0-9]/g' transcriptionFile.txt > outtranscriptionFile.txt
I get output but puts in my RegExp in place where timecode is supposed to be. Any ideas? Also How do I can trim last 3 digits off far right side of timecode before I update file?
Any tips or suggestions will be much appreciated.
Thanks :-)
With GNU sed:
$ sed -r 's/^([0-9]{2}:[0-9]{2}:[0-9]{2})\>\.[0-9]{2}/\1/' transcriptionFile.txt
00:50:34>INTERVIEWER
Why was it ............... script?
00:50:35>JOHN DOE
Because of the quality.
To edit the file in place, add the -i option:
sed -r -i 's/^([0-9]{2}:[0-9]{2}:[0-9]{2})\>\.[0-9]{2}/\1/' transcriptionFile.txt
Explanation:
[0-9]{2}: matches every two digits followed by a :. All three occurences are captured using brackets.
\>\.[0-9]{2} matches > followed by a dot and two digits.
using backreference \1, strings matching previous pattern are replaced with captured characters (timecode without milliseconds).
I'm running Windows and have the GnuWin32 toolkit, which includes sed. Specifically:
C:\TEMP>sed --version
GNU sed version 4.2.1
I have a text file with two sections: A fixed part I want to preserve, and a part that's appended after running a job.
In the file is a unique string that identifies the start of the part that's added, and I'd like to use Gnu sed to isolate only the part of the file that's before the unique string - i.e., so I can append different data to the fixed part each time the job is run.
I know I could keep the fixed portion in a separate file, but that adds complexity and it would be more elegant if I could just reuse the data at the start of the same file.
A long time ago I knew how to set up sed scripts, and I'm sure this can be done with sed, but I've slept since then. :)
Can you please describe how to use sed to display the lines of text in a file up to and not including a specific string?
Example:
line 1 of fixed portion
line 2 of fixed portion
unique string
line 1 of appended portion
line 2 of appended portion
line 3 of appended portion
What I'd like is to see as output:
line 1 of fixed portion
line 2 of fixed portion
I've gotten as far as:
sed -r -n -e "0,/unique string/p"
but that prints the unique string as well.
Thanks in advance.
-Noel
This should work for you:
sed -n '/unique string/q;p' file
It quits processing at unique string. Other lines get printed.
An alternative might be to use a range address like this:
sed -n '1,/unique string/{/unique string/!p}' file
Note that sed includes the range border. We need to exclude unique string from printing.
Furthermore I'm using the -n option which makes sed suppress the output of input lines by default.
One thing, if unique string can contain characters which are also syntax characters in the regex like ...
test*
... sed might not be the right tool for the job any more since it can only match regular expressions but not fixed strings.
In that case awk might be the tool of choice:
awk 'index("*unique string*"){exit}1' file
index("string") returns a non zero value (the position) if the string has been found. We cancel further processing of input lines in that case and don't print that line as well.
The trailing 1 always evaluates to true and makes awk print all the lines until the previous condition applies.
I have a list of usernames and i would like add possible combinations to it.
Example. Lets say this is the list I have
johna
maryb
charlesc
Is there is a way to use sed to edit it the way it looks like
ajohn
bmary
ccharles
And also
john_a
mary_b
charles_c
etc...
Can anyone assist me into getting the commands to do so, any explanation will be awesome as well. I would like to understand how it works if possible. I usually get confused when I see things like 's/\.(.*.... without knowing what some of those mean... anyway thanks in advance.
EDIT ... I change the username
sed s/\(user\)\(.\)/\2\1/
Breakdown:
sed s/string/replacement/ will replace all instances of string with replacement.
Then, string in that sed expression is \(user\)\(.\). This can be broken down into two
parts: \(user\) and \(.\). Each of these is a capture group - bracketed by \( \). That means that once we've matched something with them, we can reuse it in the replacement string.
\(user\) matches, surprisingly enough, the user part of the string. \(.\) matches any single character - that's what the . means. Then, you have two captured groups - user and a (or b or c).
The replacement part just uses these to recreate the pattern a little differently. \2\1 says "print the second capture group, then the first capture group". Which in this case, will print out auser - since we matched user and a with each group.
ex:
$ echo "usera
> userb
> userc" | sed "s/\(user\)\(.\)/\2\1/"
auser
buser
cuser
You can change the \2\1 to use any string you want - ie. \2_\1 will give a_user, b_user, c_user.
Also, in order to match any preceding string (not just "user"), just replace the \(user\) with \(.*\). Ex:
$ echo "marya
> johnb
> alfredc" | sed "s/\(.*\)\(.\)/\2\1/"
amary
bjohn
calfred
here's a partial answer to what is probably the easy part. To use sed to change usera to user_a you could use:
sed 's/user/user_/' temp
where temp is the name of the file that contains your initial list of usernames. How this works: It is finding the first instance of "user" on each line and replacing it with "user_"
Similarly for your dot example:
sed 's/user/user./' temp
will replace the first instance of "user" on each line with "user."
Sed does not offer non-greedy regex, so I suggest perl:
perl -pe 's/(.*?)(.)$/$2$1/g' file
ajohn
bmary
ccharles
perl -pe 's/(.*?)(.)$/$1_$2/g' file
john_a
mary_b
charles_c
That way you don't need to know the username before hand.
Simple solution using awk
awk '{a=$NF;$NF="";$0=a$0}1' FS="" OFS="" file
ajohn
bmary
ccharles
and
awk '{a=$NF;$NF="";$0=$0"_" a}1' FS="" OFS="" file
john_a
mary_b
charles_c
By setting FS to nothing, every letter is a field in awk. You can then easy manipulate it.
And no need to using capturing groups etc, just plain field swapping.
This might work for you (GNU sed):
sed -r 's/^([^_]*)_?(.)$/\2\1/' file
This matches any charactes other than underscores (in the first back reference (\1)), a possible underscore and the last character (in the second back reference (\2)) and swaps them around.
I'm trying to extract a list of CentOS domain names only from http://mirrorlist.centos.org/?release=6.4&arch=x86_64&repo=os
Truncating prefix "http://" and "ftp://" to the first "/" character only resulting a list of
yum.phx.singlehop.com
mirror.nyi.net
bay.uchicago.edu
centos.mirror.constant.com
mirror.teklinks.com
centos.mirror.netriplex.com
centos.someimage.com
mirror.sanctuaryhost.com
mirrors.cat.pdx.edu
mirrors.tummy.com
I searched stackoverflow for the sed method but I'm still having trouble.
I tried doing this with sed
curl "http://mirrorlist.centos.org/?release=6.4&arch=x86_64&repo=os" | sed '/:\/\//,/\//p'
but doesn't look like it is doing anything. Can you give me some advice?
Here you go:
curl "http://mirrorlist.centos.org/?release=6.4&arch=x86_64&repo=os" | sed -e 's?.*://??' -e 's?/.*??'
Your sed was completely wrong:
/x/,/y/ is a range. It selects multiple lines, from a line matching /x/ until a line matching /y/
The p command prints the selected range
Since all lines match both the start and end pattern you used, you effectively selected all lines. And, since sed echoes the input by default, the p command results in duplicated lines (all lines printed twice).
In my fix:
I used s??? instead of s/// because this way I didn't need to escape all the / in the patterns, so it's a bit more readable this way
I used two expressions with the -e flag:
s?.*://?? matches everything up until :// and replaces it with nothing
s?/.*?? matches everything from / until the end replaces it with nothing
The two expressions are executed in the given order
In modern versions of sed you can omit -e and separate the two expressions with ;. I stick to using -e because it's more portable.