Solved

how to export a pipe variable directly to shell command

Posted on 2014-09-05
9
218 Views
Last Modified: 2014-09-06
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
>
0
Comment
Question by:KCITS
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
`sed '1,/firstmatch/d;/secondmatch/,$d'  file`
0
 
LVL 1

Author Comment

by:KCITS
Comment Utility
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
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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?
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
sed '1,/firstmatch/d;/secondmatch/,$d' /path/to/your/file | xargs
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 84

Assisted Solution

by:ozo
ozo earned 167 total points
Comment Utility
doesn't xargs with no utility specified default to  | xargs echo

`sed '1,/firstmatch/d;/secondmatch/,$d'  /path/to/your/file`
or
$(sed '1,/firstmatch/d;/secondmatch/,$d'  /path/to/your/file)
should execute the command directly
0
 
LVL 48

Accepted Solution

by:
Tintin earned 167 total points
Comment Utility
Ah yes, correct ozo.

Meant to write

sed '1,/firstmatch/d;/secondmatch/,$d'  /path/to/your/file | bash
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
That could be better than `` or $() if there are multiple commands between your expressions
0
 
LVL 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 166 total points
Comment Utility
You can also call it eplicitly like this, supposing your shell is bash:

bash -c "$(sed '1,/firstmatch/d;/secondmatch/,$d' /path/to/your/file)"
0
 
LVL 1

Author Closing Comment

by:KCITS
Comment Utility
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now