Sed: Insert text before a random text - linux

I am having problems using sed to change this:
script_summary("Short random text");
script_id(#12345);
Into this:
script_tag(name:"text", value:"Short random text");
script_oid("1.3.6.1.4.1.25623.1.0.#12345");
Could you please, point me out in the right direction?
Thank you very much in advance.
Best regards,

What about:
Using 2 iterations. Testing it:
echo 'script_summary("Short random text");' | sed -e 's/script_summary("\(.*\)");/script_tag(name:"text", value:"\1");/'
echo 'script_id(#12345);' | sed -e 's/script_id(\(#[0-9]\+\));/script_oid("1.3.6.1.4.1.25623.1.0.\1");/'
So you may want to use it with
sed -i -e <regex1> <file>
sed -i -e <regex2> <file>
or
cat <file> | sed -e <regex1> | sed -e <regex2>
depends on how big the files is, and what you want to do with it.

Related

sed continuous digit only and remove anything longer and shorter

i try to extract continuous digit, only with 5 continuous digit, i want to remove all letters, any other digit that is not 5 continuously.
echo ajik84928ijs23d8ff89fj | sed 's/[^0-9]*//g' > answer.txt
echo ajik84928ijs23d8ff89fj | grep -E -o "[0-9]+"
echo ajik84928ijs23d8ff89fj| tr -d [a-z]
i only want 84928, but i can only get 8492823889, i seached many posts but still the closest answer is showed above, thanks for your help. anything such as sed, tr, grep, pearl is going to help, thanks!
With GNU sed:
echo ajik84928ijs23d8ff89fj | sed -r 's/.*([0-9]{5}).*/\1/'
With GNU grep:
echo ajik84928ijs23d8ff89fj | grep -E -o "[0-9]{5}"
Output:
84928
grep approach to match only 5-digit numbers(not less and not more):
echo "ajik84928ijs23d8ff89fj111111123" | grep -Po '(^|[^0-9])\K[0-9]{5}(?=[^0-9]|$)'
84928

SED: Displaying the first 10 lines of sophisticated expression

How to use sed to find lines with the word linux? As later display a first line 10 with the word linux?
EX.:
cat file | sed -e '/linux/!d' -e '10!d' ### I can not display the first 10 lines of the word linux
cat file | sed '/linux/!d' | sed '10!d' ### It is well
How to make it work with one sed?
cat file | sed -e '/linux/!d; ...?; 10!d'
...? - storing of the buffer linux? 10 later cut the lines?
Someone explain to me?
I would use awk:
awk '/linux/ && c<10 {print;c++} c==10 {exit}' file
This might work for you (GNU sed):
sed -nr '/linux/{p;G;/(.*\n){10}/q;h}' file
Print the line if it contains the required string. If the required number of lines has already been printed quit, otherwise store the line and previous lines in the hold space.
You could use perl:
perl -ne 'if (/linux/) {print; ++$a;}; last if $a==10' inputfile
Using GNU sed:
sed -rn "/linux/{p;x;s/^/P/;ta;:a;s/^P{10}$//;x;Tb;Q;:b}" filename
Thanks. You are great. All of the examples look very nice. Wooow :) It is a pity that I can not do that.
I have not seen for 'r' option in sed. I need to learn.
echo -e 'windows\nlinux\nwindows\nlinux\nlinux\nwindows' | sed -nr '/linux/{p;G;/(.*\n){2}/q;h}'
It works very well.
echo -e 'windows\nlinux\nwindows\nlinux\nlinux\nwindows' | sed -nr '/linux/{p;G;/(.*\n){2}/q;h}' | sed '2s/linux/debian/'
Can I ask you one more example? How to get a result at one sed?

linux truncate first line - is there a better method

There is the following which will truncate a file to take the first line and overwrite it,
I wonder if there is a cleaner way then to do this:
touch temp.txt; cat versions.txt | head -1 > temp.txt; mv temp.txt versions.txt
Note that this does not work:
cat versions.txt | head -1 > versions.txt
and the touch is not necessary on most systems
Here's one way of doing it in-place
ex -c ':2,$d' -c ':wq' versions.txt
You could do it with sed in one command:
sed -i -n 1p versions.txt
Use sed like this:
sed -i.bak '1d' file
Ho about this
ONE=`awk 'NR==1 {print;exit}' versions.txt`&& echo $ONE>versions.txt

How do I get sed to read from standard input? [duplicate]

This question already has answers here:
sed unknown option to `s' in bash script [duplicate]
(4 answers)
Closed 2 years ago.
I am trying
grep searchterm myfile.csv | sed 's/replaceme/withthis/g'
and getting
unknown option to `s'
What am I doing wrong?
Edit:
As per the comments the code is actually correct. My full code resembled something like the following
grep searchterm myfile.csv | sed 's/replaceme/withthis/g'
# my comment
And it appears that for some reason my comment was being fed as input into sed. Very strange.
use the --expression option
grep searchterm myfile.csv | sed --expression='s/replaceme/withthis/g'
use "-e" to specify the sed-expression
cat input.txt | sed -e 's/foo/bar/g'
To make sed catch from stdin , instead of from a file, you should use -e.
Like this:
curl -k -u admin:admin https://$HOSTNAME:9070/api/tm/3.8/status/$HOSTNAME/statistics/traffic_ips/trafc_ip/ | sed -e 's/["{}]//g' |sed -e 's/[]]//g' |sed -e 's/[\[]//g' |awk 'BEGIN{FS=":"} {print $4}'
If you are trying to do an in-place update of text within a file, this is much easier to reason about in my mind.
grep -Rl text_to_find directory_to_search 2>/dev/null | while read line; do sed -i 's/text_to_find/replacement_text/g' $line; done
Open the file using vi myfile.csv
Press Escape
Type :%s/replaceme/withthis/
Type :wq and press Enter
Now you will have the new pattern in your file.

How to filter data out of tabulated stdout stream in Bash?

Here's what output looks like, basically:
? RESTRequestParamObj.cpp
? plugins/dupfields2/_DupFields.cpp
? plugins/dupfields2/_DupFields.h
I need to get the filenames from second column and pass them to rm. There's AWK script that goes like awk '{print $2}' but I was wondering if there's another solution.
If you have spaces between the ? and the filename then:
cut -c9-
If they're tabs then:
cut -f2
Placed your output in file
$> cat ./text
? RESTRequestParamObj.cpp
? plugins/dupfields2/_DupFields.cpp
? plugins/dupfields2/_DupFields.h
Edit it with sed
$> cat ./text | sed -r -e 's/(\?[\ \t]*)(.*)/\2/g'
RESTRequestParamObj.cpp
plugins/dupfields2/_DupFields.cpp
plugins/dupfields2/_DupFields.h
Sed in here is matching 2 parts of line -
? with tabs or spaces
Other characters until the end f the line
And then it changes whole line only with second part.
This might work for you:
echo "? RESTRequestParamObj.cpp" | sed -e 's/^\S\+/rm /' | sh
or using GNU sed
echo "? RESTRequestParamObj.cpp"| sed -r 's/^\S+/rm /e'
bash only solution, assuming your output comes from stdin:
while read line; do echo ${line##* }; done
use cut/perl instead
cut -f2 -t'\t'|xargs rm -rf
<your output>|perl -ne '#cols = split /\t/; print $cols[1]'|xargs rm -rf

Resources