Link to home
Start Free TrialLog in
Avatar of KCITS
KCITSFlag for Australia

asked on

how to export a pipe variable directly to shell command

Hi
I am working on a project to grep/awk/sed a file and directly process a command between two expressions.

for instance if the file reads

XXXXXXXX YYYYY ZZZZZ
firstmatch
du -h /home  
secondmatch
AAAAA BBBBBBB CCCC

I can extract the information I need (du -h /home) but can not make it run directly in a shell

sed '1,/firstmatch/d;/secondmatch/,$d |

I just end up with
>
Avatar of ozo
ozo
Flag of United States of America image

`sed '1,/firstmatch/d;/secondmatch/,$d'  file`
Avatar of KCITS

ASKER

Hi ozo
Not sure what your suggestion is.
isn't the command 'file' just for determining a file type?

I can export the sed result to a file with
sed '1,/firstmatch/d;/secondmatch/,$d'  > /home/testfile.sh
and then run it, but I was hoping to just output and run a single command directly
file was meant to be the name of your file containing
XXXXXXXX YYYYY ZZZZZ
firstmatch
du -h /home  
secondmatch
AAAAA BBBBBBB CCCC


you can output to a file with > /home/testfile.sh
but where are you telling sed where to get the input from?
Avatar of Tintin
Tintin

sed '1,/firstmatch/d;/secondmatch/,$d' /path/to/your/file | xargs
SOLUTION
Avatar of ozo
ozo
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
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
That could be better than `` or $() if there are multiple commands between your expressions
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
Avatar of KCITS

ASKER

Thanks Guys

sorry my typo in the above expressions I accidentally omitted my source file, it should have read  
sed '1,/firstmatch/d;/secondmatch/,$d'  /home/testfile.txt|

all accepted solutions worked well, I also experimented and tried the below as an alternative, it worked also.

sed '1,/firstmatch/d;/secondmatch/,$d'  /path/to/your/file | sh  

cheers