Solved

how to export a pipe variable directly to shell command

Posted on 2014-09-05
9
221 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
ID: 40307067
`sed '1,/firstmatch/d;/secondmatch/,$d'  file`
0
 
LVL 1

Author Comment

by:KCITS
ID: 40307157
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
ID: 40307165
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 48

Expert Comment

by:Tintin
ID: 40307167
sed '1,/firstmatch/d;/secondmatch/,$d' /path/to/your/file | xargs
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 167 total points
ID: 40307172
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
ID: 40307178
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
ID: 40307193
That could be better than `` or $() if there are multiple commands between your expressions
0
 
LVL 37

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 166 total points
ID: 40307759
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
ID: 40308283
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

773 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