?
Solved

how to export a pipe variable directly to shell command

Posted on 2014-09-05
9
Medium Priority
?
231 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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
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 668 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 668 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 664 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 a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
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.
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

800 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