Solved

how to export a pipe variable directly to shell command

Posted on 2014-09-05
9
219 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
 
LVL 48

Expert Comment

by:Tintin
ID: 40307167
sed '1,/firstmatch/d;/secondmatch/,$d' /path/to/your/file | xargs
0
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.

 
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
Gerwin Jansen 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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-…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now