Sponsored Content
Full Discussion: Execution Problems with sed
Homework and Emergencies Homework & Coursework Questions Execution Problems with sed Post 302532595 by bakunin on Tuesday 21st of June 2011 10:57:53 AM
Old 06-21-2011
Quote:
Originally Posted by aggie6970
2. Make a list of files in /usr/bin that have the letter "a" as the second character. Put the result in a temporary file.
Lets go over this as you have already found out you are doing something wrong. Your code:

Code:
ls /usr/bin/?a* | cp /usr/bin/?a* /tmp

It pays to examine a complicated pipeline-command step by step. You already found out the correct filemask "?a*" to filter out the files you are interested in. Now execute the command

Code:
ls /usr/bin/?a*

and have a look at what it does. To get a more verbose listing let us use the "-l" option of "ls":

Code:
ls -l /usr/bin/?a*

You wanted a "list of the files ...". Is this your list? Do you need to work on the list any further? If "no" you are finished. Just save the result to a file using redirection. You know about redirection, yes? (If not: just ask.)

If "yes", you need to still work on the list, lets have a look at the second part of your command:

Code:
| cp /usr/bin/?a* /tmp

Let us first talk about pipelines ("|"): Unix processes are like garden hoses with the data acting as water: you fill something in, it is in some way worked on inside, finally something (else) comes out. Where the data goes in is a special device called "stdin" where data comes out is called "stdout". Every process has these facilities. What a pipeline does is simple: it connects the <stdout> of the first process to the <stdin> of the second.

Here is an example on how this works: consider the command "echo", which displays text. Enter

Code:
echo abcdefg

on the commandline to have "abcdefg" displayed. In fact it works this way: "echo" puts everything given as commandline option to it (the text) to its <stdout>, which happens to point to the display *). Therefore the text gets displayed. Could the output be rerouted? Oh, yes! We can use this data stream to put it to the next utility, lets say "tr". "tr" works as a text filter and gets two arguments: a character it searches for in the datastream and a character it replaces the character with.**)

Now lets try to use this knowledge in a pipeline:

Code:
echo abcdefg | tr a Z

It works like this: echo produces its data stream "a-b-c-...." and puts it to <stdout>. As this is connected to "tr"s <stdin> it is not displayed but "tr" works on it, replacing every "a" with a "Z". Then the resulting data stream "Z-b-c-...." is put to "tr"s <stdout>. As this is not redirected it points to the display and hence it is displayed.

Now lets reexamine what you have done: you produced a data stream consisting of some file names with "ls". Then you piped these to "cp", a utility which copies files. "cp" does not know how to work on datastreams because it only works on commandline options. So "cp" simply throws away the "garbage" input it can't work with and examines its command line:

Code:
cp /usr/bin/?a* /tmp

This tells "cp" to select all files of the form "/usr/bin/?a*" and copy these to "/tmp". Exactly this is what happened - it did copy the files.

I hope this helps.

bakunin
_________
*) I skip the details of device handling here to make it simple. For the purpose of the problem at hand this simplification is sufficient.

**) In fact "tr" can do more, but this would just confuse things.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

execution problems with curl

I have been getting error "curl: (7) Failed to connect to IP number 1" when using the CURL command Could someone help??? (1 Reply)
Discussion started by: infernalhell
1 Replies

2. Programming

execution problems with cron

how to store a date into file? and how we can access date from the file? ---------- Post updated at 06:09 AM ---------- Previous update was at 06:08 AM ---------- how we can store date in file? (1 Reply)
Discussion started by: causalmodi777
1 Replies

3. UNIX and Linux Applications

Execution Problems with Cron

Hi all!! I have a nerve-wracking concept (probably for me!!) which is not understood. My crontab entry looks this way. 33 09 22 3 * /home/myexp.sh "Bgp4 ALL" >/dev/null 2>&1 But cron gets started occasionally. Sometimes it does. Sometimes it does not. And sometimes it hangs in the middle (I... (1 Reply)
Discussion started by: dhivyasuresh
1 Replies

4. Shell Programming and Scripting

Execution Problems!!

i have been working on this for a about 12 hours today say's end of file un expected any idea's using the bourne shell and its driving me nuts worked fine in bash but prof says make it work in bourne and good luck worth 13% any help would be awesome #!/bin/sh trap "rm mnt2/source/tmp/* 2>... (1 Reply)
Discussion started by: mrhiab
1 Replies

5. UNIX for Dummies Questions & Answers

Execution Problems with Cron

Hi friends, today i created a cron job , registered the crontab file associated but dont know why the cron is not getting executed at the right time.?? content of Crontab file : 21 15 * * * /subrat/myt i wanted to execute the script myt on 15:21 PM of everyday. the script myt... (2 Replies)
Discussion started by: paras.oriental
2 Replies

6. Shell Programming and Scripting

Execution Problems

this my source file ************* fixed *************** Begin equipmentId : d9 processor : fox number : bhhhhhh Variable # 1: Id : 100 Type : 9 nType : s gType : 5f mType : 4 LField : England DataField : london Length ... (6 Replies)
Discussion started by: teefa
6 Replies

7. Shell Programming and Scripting

Execution problems with sed

Hi,I confused how to use sed to deal with big file. example: the big file have some different urls just with filename. how can i use sed to fetch url except file name and replace to other urls with filename? thanks!!! (11 Replies)
Discussion started by: hshzh359
11 Replies

8. UNIX for Dummies Questions & Answers

Execution Problems with Cron

Good evening, ive got this cron to be run: if i run this manually it doesnt work,it takes me to the prompt again /export/app/CO/opge/scr/Informe_parametros_colombia.ksh >/dev/null 2>&1 here is the code fragment: coopge@coopge: opge PRODUCCION>more... (1 Reply)
Discussion started by: alexcol
1 Replies

9. HP-UX

Execution problems with swreg

HP UX 10.20 I have a directory "/var/spool/sw" which is supposed to be a "Depot" directory. Turns out it is not and when using the swreg command "swreg -l /var/spool/sw" I get errors in part ERROR for option "-l /var/spool/sw" keyword or it's value may be incorrect or the keyword does not apply... (3 Replies)
Discussion started by: Randydog
3 Replies

10. UNIX for Beginners Questions & Answers

Execution problems

How to find a word in a directory which contains many files? i just want to count how many such words are present in all the files? This is the code which i tried for a single file echo "Enter the file name:" read file echo "Enter the word to search:" read word if then echo "The count... (4 Replies)
Discussion started by: Meeran Rizvi
4 Replies
All times are GMT -4. The time now is 04:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy