Removing matching text from line - sed

I have a example cut down from a log file.
112 172.172.172.1#50912 (ssl.bing.com):
I would like some how to remove the # and numbers after and (): from the url.
Would like the result.
112 172.172.172.1 ssl.bing.com
Here is the sed oneliner I have been working on.
cat newdns.log | sed -e 's/.*query: //' | cut -f 1 -d' ' | sort | uniq -c | sort -k2 > old.log
Thanks

Using sed, you could say:
sed 's/#[0-9]*//;s/(\(.*\)):$/\1/' filename
or, in a single substitution:
sed 's/#[0-9]* *(\(.*\)):$/ \1/' filename

Another sed:
sed -r 's/#[^ ]+|[():]//g'
$ echo '112 172.172.172.1#50912 (ssl.bing.com):' | sed -r 's/#[^ ]+|[():]//g'
112 172.172.172.1 ssl.bing.com

Related

grep + grep + sed = sed: no input files

Can anybody help me please?
grep " 287 " file.txt | grep "HI" | sed -i 's/HIS/HID/g'
sed: no input files
Tried also xargs
grep " 287 " file.txt | grep HI | xargs sed -i 's/HIS/HID/g'
sed: invalid option -- '6'
This works fine
grep " 287 " file.txt | grep HI
If you want to keep your pipeline:
f=file.txt
tmp=$(mktemp)
grep " 287 " "$f" | grep "HI" | sed 's/HIS/HID/g' > "$tmp" && mv "$tmp" "$f"
Or, simplify:
sed -i -n '/ 287 / {/HI/ s/HIS/HID/p}' file.txt
That will filter out any line that does not contain " 287 " and "HI" -- is that what you want? I suspect you really want this:
sed -i '/ 287 / {/HI/ s/HIS/HID/}' file.txt
For lines that match / 287 /, execute the commands in braces. In there, for lines that match /HI/, search for the first "HIS" and replace with "HID". sed implicitly prints all lines if -n is not specified.
Other commands that do the same thing:
awk '/ 287 / && /HI/ {sub(/HIS/, "HID")} {print}' file.txt > new.txt
perl -i -pe '/ 287 / and /HI/ and s/HIS/HID/' file.txt
awk does not have an "in-place" option (except gawk -i inplace for recent gawk versions)

perl - Extract data using grep and sed

I'm using this code to get all titles from urls with http://something.txt:
#!/usr/bin/perl -w
$output = `cat source.html | grep -o '<a .*href=.*>' | grep -E 'txt' | sed -e 's/<a /\n<a /g' | sed -e 's/<a .*title="//' | cut -f1 -d '"'`;
print("$output");
When i run this on perl i get the error:
sed: -e expression #1, char 6: unterminated `s' command
The error is related with this portion of code:
sed -e 's/<a /\n<a /g'
In backquotes, Perl uses the same rules as in double quotes. Therefore, \n corresponds to a newline; you have to backslash the backslash to pass literal \ to the shell:
`sed -e 's/<a /\\n<a /g'`

Solaris sed label too long

I am trying to execute a shell file, in which there is a line:
sed -ne ':1;/PinnInstitutionPath/{n;p;b1}' Institution | sed -e s/\ //g | sed -e s/\=//g | sed -e s/\;//g | sed -e s/\"//g | sed -e s/\Name//g
And un error message turns out : "Label too long: :1;/PinnInstitutionPath/{n;p;b1}"
I am a noob at linux, so can anyone help me to solve this problem, thank you!
Try changing
sed -ne ':1;/PinnInstitutionPath/{n;p;b1}'
to
sed -ne ':1' -e '/PinnInstitutionPath/{n;p;b1}'
Also, you don't need to call sed so many times:
sed -ne 's/[ =;"]//g; s/Name//g' -e ':1' -e '/PinnInstitutionPath/{n;p;b1}'
Concerning 'sed: Label too long' in Solaris (SunOS) - you will need to split your command into several lines, if you use labels.
In your casesed -ne ':1
/PinnInstitutionPath/{
n
p
b 1
}' Institution | sed -e s/\ //g -e s/\=//g -e s/\;//g -e s/\"//g -e s/\Name//g

Pattern extraction using SED or AWK

How do I extract 68 from v1+r0.68?
Using awk, returns everything after the last '.'
echo "v1+r0.68" | awk -F. '{print $NF}'
Using sed to get the number after the last dot:
echo 'v1+r0.68' | sed 's/.*[.]\([0-9][0-9]*\)$/\1/'
grep is good at extracting things:
kent$ echo " v1+r0.68"|grep -oE "[0-9]+$"
68
Match the digit string before the end of the line using grep:
$ echo 'v1+r0.68' | grep -Eo '[0-9]+$'
68
Or match any digits after a .
$ echo 'v1+r0.68' | grep -Po '(?<=\.)\d+'
68
Print everything after the . with awk:
echo "v1+r0.68" | awk -F. '{print $NF}'
68
Substitute everything before the . with sed:
echo "v1+r0.68" | sed 's/.*\.//'
68
type man grep
and you will see
...
-o, --only-matching
Show only the part of a matching line that matches PATTERN.
then type echo 'v1+r0.68' | grep -o '68'
if you want it any where special do:
echo 'v1+r0.68' | grep -o '68' > anyWhereSpecial.file_ending

awk, sed: one liner command for removing spaces from _all_ file names in a given folder?

Before:
eng-vshakya:scripts vshakya$ ls
American Samoa.png Faroe Islands.png Saint Barthelemy.png
After:
eng-vshakya:scripts vshakya$ ls
AmericanSamoa.png FaroeIslands.png SaintBarthelemy.png
Tried below prototype, but it does not work :( Sorry, not very good when it comes to awk/sed :(
ls *.png | sed 's/\ /\\\ /g' | awk '{print("mv "$1" "$1)}'
[ Above is prototype, real command, I guess, would be:
ls *.png | sed 's/\ /\\\ /g' | awk '{print("mv "$1" "$1)}' | sed 's/\ //g'
]
No need to use awk or sed when you can do this in pure bash.
[ghoti#pc ~/tmp1]$ ls -l
total 2
-rw-r--r-- 1 ghoti wheel 0 Aug 1 01:19 American Samoa.png
-rw-r--r-- 1 ghoti wheel 0 Aug 1 01:19 Faroe Islands.png
-rw-r--r-- 1 ghoti wheel 0 Aug 1 01:19 Saint Barthelemy.png
[ghoti#pc ~/tmp1]$ for name in *\ *; do mv -v "$name" "${name// /}"; done
American Samoa.png -> AmericanSamoa.png
Faroe Islands.png -> FaroeIslands.png
Saint Barthelemy.png -> SaintBarthelemy.png
[ghoti#pc ~/tmp1]$
Note that the ${foo/ /} notation is bash, and does not work in classic Bourne shell.
ghoti's solution is the right thing to do. Since you ask how to do it in sed, here's one way:
for file in *; do newfile=$( echo "$file" | tr -d \\n | sed 's/ //g' );
test "$file" != "$newfile" && mv "$file" "$newfile"; done
The tr is there to remove newlines in the filename, and is necessary to ensure that sed sees the entire filename in one line.