Execution Problems with sed

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Execution Problems with sed
# 1  
Old 06-18-2011
Execution Problems with sed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
Okay so I am taking bash scripting this semester and we are now working on the stream editor sed. For our book we are using Bash Guide for Beginners

In Chapter 5 exercise numbers
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.

3. Delete the first 3 lines of each temporary file[/B]

I did solution number 2 I just have to figure out solution number 3
On solution number 2 I typed in

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


2. Relevant commands, code, scripts, algorithms:

to delete a line using sed you type in
sed '3d' example.txt



3. The attempts at a solution (include all code and scripts):

So If I type

sed '3d' /tmp

It dont work for the files in the directory
I cant figure out how to do it for all the files in directory at the same time.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Oklahoma State University Institute of Technology
Okmulgee, Oklahoma
U.S.A
Professor: Mr Fil Guinn
Course Number: ITD-2313

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

---------- Post updated at 10:13 AM ---------- Previous update was at 10:08 AM ----------

I think I may have messed up I put it in a directory instead of a file
# 2  
Old 06-19-2011
That is correct. You needed to pass a filename to sed - not a directory.
# 3  
Old 06-21-2011
Go into the directory and run this

# for file in *; do sed '3d' $file; done

Hope it Helps.
Cheers!
# 4  
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.
# 5  
Old 06-27-2011
Thank you for the info bakunin. This is a hard course for me since I have no programming background. It is fun to learn but frustrating at times.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question