search some text in csv file [duplicate] - perl

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

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.

Linux shell script, parsing each line [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am facing a problem with my shell script (I'm using SH):
I have a file with multiple line including mail adressess, for example:
abcd
plm
name_aA.2isurnamec#Text.com -> this is a line that checks the correct condition
random efgh
aaaaaa
naaame_aB.3isurnamec#Text.ro ->same (this is not part of the file)
I have used grep to filter the correct mail adresses like this:
grep -E '^[a-z][a-zA-Z_]*.[0-9][a-zA-Z0-9]+#[A-Z][A-Z0-9]{,12}.(ro|com|eu)$' file.txt
I have to write a shell that cheks the file and prints the following (for the above example it would be like this ):
"Incorrect:" abcd
"Incorrect:" plm
"Correct:" name_aA.2isurnamec#Text.com
"Incorrect:" random efgh
"Incorrect:" aaaaaa
"Correct:" naaame_aB.3isurnamec#Text.ro
I want to solve this problem using grep or sed, while, if, or pipes etc i dont want to use lists or other things.
I have tried using something like this
grep condition abc.txt | while read -r line ; do
echo "Processing $line"
# your code goes here
done
but it only prints the correct lines, and i know that i can also print the lines that dont match the grep condition using -v on grep, but i want to print the lines in the order they appear in the text file.
I'm having trouble trying to parse each line of the file, or maybe i don't need to parse the lines 1
by 1, i really dont know how to solve it.
If you could help me i would appreciate it.
Thanks
#!/bin/bash
pattern='^[a-z][a-zA-Z_]*\.[0-9][a-zA-Z0-9]+#[A-Z][A-Za-z0-9]{,12}\.(ro|com|eu)$'
while read line; do
if [ "$line" ]; then
if echo "$line" | grep -E -q $pattern; then
echo "\"Correct:\" $line"
else
echo "\"Incorrect:\" $line"
fi
fi
done
Invoke like this, assuming the bash script is called filter and the text file, text.txt: ./filter < text.txt.
Note that the full stops in the regular expression are escaped and that the domain name can contain lowercase letters (although, I think that your regex is too restrictive). Other characters are not escaped because the string is in single quotes.
while reads the standard input line by line into $line; the first if skips the empty lines; the second one checks $line against $pattern (-q suppresses grep output).

How to combine the output of regex and print in perl?

Hi I have search result as,
"abc"
from
perl -lne 'print for /"name":"(.+?)"/g' file > newfile
and
"def"
from
perl -lne 'print for /"title":"(.+?)"/g' file > newfile
I'm trying to get the O/p as
abc:"def",
by combining both one liners. I tried with:
perl -lne 'print for /"name":"(.+?)","title":"(.+?)"/g' *.json > newfile11
but it didn't work
I think I figured this out based on the input from your other question.
I'm assuming you have this as input:
{"card":{"cardName":"10AN10G","portSignalRates":["10AN10G-1-OTU2","10AN10G-1-OTU2E","10AN10G-1-TENGIGE","10AN10G-1-STM64"],"listOfPort":{"10AN10G-1-OTU2":{"portAid":"10AN10G-1-OTU2","signalType":"OTU2","tabNames":["PortDetails"],"requestType":{"PortDetails":"PTP"},"paramDetailsMap":{"PortDetails":[{"type":"dijit.form.TextBox","name":"signalType","title":"Signal Rate","id":"","options":[],"label":"","value":"OTU2","checked":"","enabled":"false","selected":""},{"type":"dijit.form.TextBox","name":"userLabel","title":"Description","id":"","options":[],"label":"","value":"","checked":"","enabled":"true","selected":""},{"type":"dijit.form.Select","name":"Frequency","title":"Transmit Frequency",}}}}}}
or at least a large text file containing those types of lines. You want to parse out the name and title from each line. You can do that with this one line.
Matt#MattPC ~/perl/testing/12
$ perl -ne 'if ( /"name":"([^"]+)","title":"([^"]+?)"/ ) { print $1 . ":\"" . $2 . "\",\n" }' input2.txt
which outputs:
signalType:"Signal Rate",
It works by capturing 2 group in the regex, one for the title and one for the name. The -ne flags go through each line the file and execute the code between the single quotes. $1 and $2 are the group we captured, and they are printed at the end.
Just as a tip, it is much easier to help you if you post your input, expected output, errors you ran into, and code you've tried when asking question.
edit: just wanted to put a disclaimer that it is better to parse JSON with a module, because what if you have escaped " with in a title or name? This regex wouldn't pick it up, but JSON parsers can handle those types of cases for you.

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.

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

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>