Hiro 714
asked on
Shell script question
I want to use sed command to grab some text file but not working.
./siteinfo.txt | grep -oE "<$1>.*</$1>" | sed 's/<$1>//' | sed 's/<\/$1>//'
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you. it's almost working.
Yes, $1 is a parameter and not sure where to put ().
I tried following, but not working.
Yes, $1 is a parameter and not sure where to put ().
I tried following, but not working.
at siteinfo.txt | grep -oE "<$1>(.*)</$1>"
siteinfo.txt has multi line text like<ABC>
1234
4567
</ABC>
I want to run test.sh ABC and result should be1234
4567
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry, I provided a sample on previous comment.
See if this helps
boby@sok-01:~$ cat 2.txt
<ABC>
1234
4567
</ABC>
Hello
boby@sok-01:~$ cat 2.txt | grep -ozE "<ABC>(.*)</ABC>" | sed '/<\/ABC>/'d | sed '/<ABC>/'d
1234
4567
boby@sok-01:~$
ASKER
thank you. improved.
but i get this result.
but i get this result.
Binary file (standard input) matches
not1234
4567
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i get this result
% at siteinfo.txt
syntax error. Last token seen: s
Garbled time
use cat instead of at if you need to print content of the file.
ASKER
i get this result with cat
% sh test3.sh ABC
Binary file (standard input) matches
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
% cat test3.sh
#!/bin/bash
cat siteinfo.txt | grep -ozE "<$1>(.*)</$1>" | sed 's/<$1>//' | sed 's/<\/$1>//'
Sorry, siteinfo.txt is private.
ASKER
% cat test3.sh
#!/bin/bash
cat siteinfo1.txt | grep -ozE "<$1>(.*)</$1>" | sed 's/<$1>//' | sed 's/<\/$1>//'
% cat siteinfo1.txt
<ABC>
eeqwerq
eafaefa
efaefaea
</ABC>
% sh test3.sh ABC
%
Now I get no result
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, but not getting the result.
% cat siteinfo1.txt
<ABC>
eeqwerq
eafaefa
efaefaea
</ABC>
% cat test3.sh
#!/bin/bash
cat siteinfo1.txt | grep -ozE "<$1>(.*)</$1>" | grep -av "$1"
% sh test3.sh ABC
%
ASKER
grep -ozE "<$1>(.*)</$1>"
this part is not working.
ASKER
% cat cat2.txt | grep -ozE "<ABC>(.*)"
<ABC>
% cat cat2.txt
<ABC>
1234
4567
</ABC>
Hello
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
is there another way to express this part?
grep -ozE "<$1>(.*)</$1>"
ASKER
I have tried other PC and your script works. Not sure why it does not work with this machine.
Check if grep version is same on both computers. Also see if data file have any differnce.
ASKER
I'm trying to do this, but still not getting the result. any advice?
cat siteinfo.txt | awk '/\<$1\>/,/\<\/$1\>
there is noway this works. you are missing a closing single quote after a closing slash.
can you provide what the input is in the text file and what it is you want to happen to the the data ?
can you provide what the input is in the text file and what it is you want to happen to the the data ?
ASKER
this worked. thank you, everyone.
cat siteinfo.txt | awk '/<'${1}'>/,/<\/'${1}'>/' | sed 's/<'${1}'>//' | sed 's/<\/'${1}'>/
I think your original problem was the single quotes on the second "sed" command. You need double quotes if you want the shell to replace variables.
Try
Open in new window