sed or perl replace characters leaving some text intact [duplicate] - perl

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
sed replace characters leaving some text intact
how to replace some characters using sed (or maybe perl), but leaving unknown number intact in.e. in file there are multiple lines like this:
<"START=xxx">
<"START=yyy">
<"START=zzz">
The 'xxx', 'yyy' and 'zzz' are different unknown values (numbers). I want to remove ending "> and replace it, also replace the beginning (but that's not to difficult for me) so in the end the file looks like this:
<START>xxx</START>
<START>yyy</START>
<START>zzz</START>
Thank you in advance!

this should do it:
sed 's;<"\([^=]\+\)=\([^"]\+\)">;<\1>\2</\1>;' file
however keep in mind that processing xml like content with line-oriented tools is not the correct way to do it, unless the format is very strict and the case focuses on a strict and well-defined subset of the formatting language.

For the fun of it, a Perl solution:
perl -pe's#<"(.+?)=(\d+)">#<$1>$2</$1>#' <file >outfile
or
perl -pie's#<"(.+?)=(\d+)">#<$1>$2</$1>#' file
for in-place replacement

awk -F"=" '{gsub(/\"|<|>/,"");print "<"$1">"$2"</"$1">"}' your_file
tested below:
> cat temp
<"START=xxx">
<"START=yyy">
<"START=zzz">
> awk -F"=" '{gsub(/\"|<|>/,"");print "<"$1">"$2"</"$1">"}' temp
<START>xxx</START>
<START>yyy</START>
<START>zzz</START>

Related

Using sed to replace value in ini config file [duplicate]

This question already has answers here:
How to escape the ampersand character while using sed
(3 answers)
Closed 2 years ago.
My config file looks like:
KEY1=VALUE1
URL=https://drive.google.com/uc?export=download&id=myhash
KEY3=VALUE3
I'm trying to use sed to replace the URL value with another one. I got to the following:
sed -i.bak 's#URL=.*#URL=https://drive.google.com/uc?export=download&id=mynewhash#g' file.txt
But that doesn't seem to work, as I'm getting:
URL=https://drive.google.com/uc?export=downloadURL=https://drive.google.com/uc?export=download&id=mynewhash=myhash
What am I missing? Thanks
& is a special character in the replacement string provided to the s command of sed. It represents the string that matches the entire regex used to search (URL=.* in your example).
In order to represent itself it needs to be escaped with \:
sed -i.bak 's#URL=.*#URL=https://drive.google.com/uc?export=download\&id=mynewhash#g' file.txt
Type man sed in your terminal to read its documentation or read the documentation of sed online.

How to reverse all words in line with sed? [duplicate]

This question already has answers here:
Reverse input order with sed
(6 answers)
Closed 4 years ago.
For example, we have:
This is the song that doesn`t end
What sed command will turn it into this?
end doesn`t that song the is This
I've found only how to reverse lines in a file (a.k.a. tac):
sed -n '1!G;h;$p'
This might work for you (GNU sed):
sed -r 'G;:a;s/^(\S+)(\s*)(.*\n)/\3\2\1/;ta;s/\n//' file
Append a newline as a delimiter. Split the current line into three and prepend the first word, the following space and the remainder of the line following the newline in that order. Iterate until the pattern matching fails and then remove the introduced newline.
Could you please try following and let me know if this helps you.
awk '{for(i=NF;i>0;i--){printf("%s%s",$i,(i>1?OFS:ORS))}}' Input_file

Using command line to find and replace? [duplicate]

This question already has answers here:
Remove everything after the first / including the first / for each line
(3 answers)
Closed 8 years ago.
I'm trying to use command line to find and replace some text. I have a file with a few million lines that are similar to this:
Something-Here/Grafton-WV</loc>
More-Information/Claremore-OK</loc>
This-Is-It/Seminole-OK</loc>
Your-Company/Lunenburg-MA</loc>
What I need to do is remove the slash and everything after it. I've done wildcard find/replace before but I'm not sure what command would need to be used to start at the slash and continue until the end of the line.
Here's what the output should be:
Something-Here
More-Information
This-Is-It
Your-Company
The following one-liner could work for you:
perl -pe 's{/.*}{}' file.txt
Explanation:
Switches:
-p: Creates a while(<>){...; print} loop for each “line” in your input file.
-e: Tells perl to execute the code on command line.
Code:
s{/.*}{}: Remove all characters after the first forward slash from the line
This is usually done with sed:
sed 's|/.*||' file.txt > newfile.txt

How to pass a variable to sed [duplicate]

This question already has answers here:
Shell variables in sed script [duplicate]
(5 answers)
Closed 8 years ago.
I want to delete words into a line. For example:
I want to delete one word in this line
And I want to delete 'one' to obtain:
I want to delete word in this line
By passing the word through a variable. So far I have got:
WORD=one ; sed -n 's/"$WORD"//g' file.txt > newfile.txt
But, it doesn't do anything. Why not? And how can I make it work?
WORD=one ; sed -e "s/$WORD//g" file.txt > newfile.txt
the key moment is variable expansion. You have to be careful though because shell variable expansion may be sometimes not what you want. In hard cases you have to do something like this:
EXPANDVAR=one; NOEXPANDVAR=another; sed -e 's/'"$EXPANDVAR"'$NOEXPANDVAR//g' file.txt > newfile.txt
In this case sed will replace (remove) pattern one$NOEXPANDVAR , literally.

search some text in csv file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Search a value in CSV
I have created a perl script.
Lets suppose I want to seach "abc" in a CSV file which contains äbcd. the script that I have written shows me abcd as a output which actually I dont want, could anyone help on this.
The issue is searching for an ANSI string in unicode file. I think you can best answer your question by reviewing this regex tutorial that points out an example similar to yours.
http://www.regular-expressions.info/unicode.html
I guess you need to match exact word in perl:
use the below regex in perl:
(/\b"your_word"\b/
tested:
without \b
> echo 'abcd'|perl -lne 'if(/abc/){print}'
abcd
with \b
> echo 'abcd' | perl -lne 'if(/\babc\b/){print}'
>
> echo 'abc' | perl -lne 'if(/\babc\b/){print}'
abc
by looking at your code you are doing this:
(grep /$curr/, #nodes)
so changing it to :
(grep /\b$curr\b/, #nodes) should work