Link to home
Start Free TrialLog in
Avatar of Hiro 714
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>//' 

Open in new window



Avatar of Yujin Boby
Yujin Boby
Flag of India image

You are using ./siteinfo.txt, that will produce error as it is a text file.

Try

cat siteinfo.txt | grep -oE "<$1>.*</$1>" | sed 's/<$1>//' | sed 's/<\/$1>//' 

Open in new window



SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hiro 714
Hiro 714

ASKER

Thank you. it's almost 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>"

Open in new window

siteinfo.txt has multi line text like
<ABC>
1234
4567
</ABC>

Open in new window

I want to run test.sh ABC and result should be
1234
4567

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:~$

Open in new window



thank you. improved.
but i get this result.
Binary file (standard input) matches

Open in new window

not
1234
4567

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i get this result
% at siteinfo.txt
syntax error. Last token seen: s
Garbled time

Open in new window

use cat instead of at if you need to print content of the file.
i get this result with cat
% sh test3.sh ABC
Binary file (standard input) matches

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
% cat test3.sh
#!/bin/bash

cat siteinfo.txt | grep -ozE "<$1>(.*)</$1>" | sed 's/<$1>//' | sed 's/<\/$1>//'

Open in new window

Sorry, siteinfo.txt is private.
% 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
%

Open in new window


Now I get no result
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
%

Open in new window


grep -ozE "<$1>(.*)</$1>"

Open in new window

this part is not working.
% cat cat2.txt | grep -ozE "<ABC>(.*)"
<ABC>
% cat cat2.txt
<ABC>
1234
4567
</ABC>
Hello

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
is there another way to express this part?
grep -ozE "<$1>(.*)</$1>"

Open in new window


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.
I'm trying to do this, but still not getting the result. any advice?
cat siteinfo.txt | awk '/\<$1\>/,/\<\/$1\>

Open in new window

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 ?
this worked. thank you, everyone.
cat siteinfo.txt | awk '/<'${1}'>/,/<\/'${1}'>/' | sed 's/<'${1}'>//' | sed 's/<\/'${1}'>/

Open in new window

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.