Solved

how to export a pipe variable directly to shell command

Posted on 2014-09-05
9
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 38

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

Suggested Solutions

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

734 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