sed & cut command issues

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions sed & cut command issues
# 1  
Old 09-29-2012
sed & cut command issues

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:
After using the egrep command to pull certain lines from the asg5f1 (creating the asg5f1c file), I am required to use the sed command in order to change all tabs in the asg5f1c file to a colon, and dump the results into a temporary file. As I understand it, in the asg5f1c file <tab> is a delimiter, and I believe I am being asked to change the delimiter from a tab to a colon.


Below is an example of what I believe the asg5f1c file should look like after the egrep command, and what the sed command will be working on (roughed-in on MS Word…).
Code:
    Employee            Hours    Rate       Hours    Rate       Gross    Net
  lname, fname    99           99.99     99           99.99     999.99   999.99
  lname, fname    99           99.99     99           99.99     999.99   999.99


After this, i am to sort the asg5f1c file and join it with another file in order to create the asg51 file.



At this point, I'm Smilie. This problem is causing a domino effect for the latter parts of the assignment, and thus holding up the whole line.


With my previous use of the sed command, I thought that the following:


Code:
sed ‘s/<tab key pressed>/:/g’ asg5f1c > tmp1

would solve my problem. The way I read the above line is “In the file asg5f1c, for all instances of a tab, substitute in a colon, and place the result into tmp1.” The above sed command outputs the following:


Code:
lname, fname:40.00:6.50: 2.00: 9.75:279.50:176.09
lname, fname:40.00:6.50:: 9.75:260.00:163.00
lname, fname:25.00:6.50:: 9.75:162.50:102.38
lname, fname   :40.00:6.50: 3.00: 9.75:289.25:182.23
lname, fname :40.00:6.50:: 9.75:260.00:163.00
lname, fname   :33.00:6.50:: 9.75:214.50:135.14
lname, fname:40.00:5.50:: 8.25:220.00:138.60
lname, fname  :40.00:5.50:: 8.25:220.00:138.60
lname, fname  :40.00:4.25:10.00: 6.38:233.75:147.26
lname, fname  :40.00:7.50::11.25:300.00:189.00

This is holding me up because my asg51 file looks the same as the above output, with the addition of a colon at the end of each line. From here, I am to cut the gross pay and net pay fields from the asg51 file. The problem being is that apparently the above "faulty" output is one field, and when I cut the pay fields:

Code:
cut -f6 asg51 > gp && cut -f7 asg51 > np

the gp & np files look like my "faulty" output, and that's when everything comes to a screeching halt.

2. Relevant commands, code, scripts, algorithms:
Unsure what to put here. Commands I am required to use are egrep, sed & cut.

3. The attempts at a solution (include all code and scripts):
I've tried sed 's/\<tab key pressed>/:/g' and sed 's/\\<tab key pressed>/:/g', but nothing works. I read & re-read the text & supplemental readings, but I can't figure it out.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
University of Maryland University College. Adelphi, MD. USA. Prof. T. Tomko. CMIS 325.

*Unable to post link to class because "You are only allowed to post URLs once you have at least 5 posts," and this is my first post.

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

Moderator's Comments:
Mod Comment edit by bakunin: Please use code tags next time for your code and data. You can post the link as text, i will put it in here for you.

Last edited by bakunin; 09-29-2012 at 11:29 AM..
# 2  
Old 09-29-2012
Quote:
Originally Posted by robrom78
As I understand it, in the asg5f1c file <tab> is a delimiter, and I believe I am being asked to change the delimiter from a tab to a colon.
You are probably right.



Quote:
Below is an example of what I believe the asg5f1c file should look like after the egrep command, and what the sed command will be working on (roughed-in on MS Word...).
Code:
  Employee        Hours        Rate      Hours        Rate      Gross    Net
  lname, fname    99           99.99     99           99.99     999.99   999.99
  lname, fname    99           99.99     99           99.99     999.99   999.99

[...]
Code:
sed ‘s/<tab key pressed>/:/g' asg5f1c > tmp1

would solve my problem. The way I read the above line is “In the file asg5f1c, for all instances of a tab, substitute in a colon, and place the result into tmp1.”
That is correct - this is what the sed command does.

Code:
lname, fname:40.00:6.50: 2.00: 9.75:279.50:176.09
lname, fname:40.00:6.50:: 9.75:260.00:163.00
lname, fname:25.00:6.50:: 9.75:162.50:102.38
lname, fname   :40.00:6.50: 3.00: 9.75:289.25:182.23
lname, fname :40.00:6.50:: 9.75:260.00:163.00
lname, fname   :33.00:6.50:: 9.75:214.50:135.14
lname, fname:40.00:5.50:: 8.25:220.00:138.60
lname, fname  :40.00:5.50:: 8.25:220.00:138.60
lname, fname  :40.00:4.25:10.00: 6.38:233.75:147.26
lname, fname  :40.00:7.50::11.25:300.00:189.00

Have a cose look at this file: in some lines there are two colons adjacent. Probably your input file, in order to look neatly formatted, uses several instead of exactly one tab character to delimit its columns - probably the number of used tabs varies from line to line to compensate for names of varying length. As your replacement is on a one-to-one basis several tabs will become as many colons.

Quote:
(roughed-in on MS Word...)
Here is my suggestion: first off, if you work for Unix, work on it. By working on another OS (to my knowledge MS-Word only works on MS-Windows) you introduce an additional complexity instead of making it easier. The graphical interrupt-handler from Redmont uses another line-delimiting schema than Unix (<CR><LF> instead of <newline>) and scripts written with a Windows-tool will not necessarily run under Unix because of this.

Furthermore, do not use text processors (not even working ones, let alone Word) to write program text. They are not built for this. They are built for formatting text and entering "flow" text (organised in paragraphs, chapters, etc.). Program text is neither formatted nor organised that way. On the other hand text processors usually fall short where the needs of programmers are concerned: automatic indenting, template editing, finding a certain line, complex search-and-replace-jobs, etc.. It is like using a Ferrari to pull a 40t-articulated lorry. True, the Ferrari has the same 500hp as the bobtail, but it is simply not built for the task to pull large amounts of cargo.

So, do yourself a favour and use "vi" or "emacs" if you prefer that or any other of the myriad of text editors available on Unix systems. I personally prefer vi for its raw power and unparalleled speed, but i admit it has a steep learning curve. All i can say is: it is a rewarding task to learn it. Usually cursing it the first 3 months you use it is the beginning of a life-long love-affair.

After these rather general and philosophical remarks something more concrete:

In "vi" you can view unprintable characters by pressing ":" (get in command mode) and then entering "set list". You will notice that every line end is now marked with "$" (the newline character) and tabs become "^I" (one character as you can see going over it with the cursor). You can switch this mode off again by entering ":set nolist". This way you can analyse your input file and find out if all your assertions about its structure are indeed correct.

If you find out that several tabs instead of exactly one are sometimes used you have to redefine what a "field delimiter" is in your file: not "one tab character", but "one or several tab characters" and you will have to modify your delimiter-translating program accordingly.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problems with ampersand (&) in sed command

Hello everybody, I have a Problem with sed command. I want to replace a defined string with a string from a database field (dynamic). e.g. sed -i -e 's/%NAME%/'"$HNAME"'/g' The Problem is that the $HNAME variable can contain Special characters like '&' e.g. HNAME="AH Kruger & Co. KG" ... (1 Reply)
Discussion started by: Bambuti2000
1 Replies

2. Shell Programming and Scripting

Cut command on RHEL 6.8 compatibility issues

We have a lot of scripts using cut as : cut -c 0-8 --works for cut (GNU coreutils) 5.97, but does not work for cut (GNU coreutils) 8.4. Gives error - cut: fields and positions are numbered from 1 Try `cut --help' for more information. The position needs to start with 1 for later... (6 Replies)
Discussion started by: Vikram Jain
6 Replies

3. Shell Programming and Scripting

Issues in sed command

I use SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux We have a user-defined command called "submit" which will open a vi terminal in which we need to enter the description at 24th line and save it. In order to simplify this, i decided to create another command in which the... (3 Replies)
Discussion started by: pandeesh
3 Replies

4. Shell Programming and Scripting

sed and cut command in variable

hi, i want to remove 12 and 13 column from psv files and dump them in new folder ls -ltr *GTDA_Dly_Pmix_*.psv>filename.xls var1=`cat filename.xls` for i in $var1 do var3=`echo "$i" |cut -d '|' -f12,13 |sort -u` sed -e 's/"|$var3"//g... (2 Replies)
Discussion started by: renuk
2 Replies

5. Shell Programming and Scripting

applescript & grep - sed command

I'm new using Unix commands in applescript. The following script you choose different folders with PDfs, get file count of PDfs on chosen folders, & write the results in text file. set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" with... (0 Replies)
Discussion started by: nellbern
0 Replies

6. UNIX for Dummies Questions & Answers

having issues in using Cut Command

Hi, I would like to ask for your help with my problem, im writing a unix script to remove characters before the symbol "/", but the one being removed are characters after the symbol. Ive searched over the internet and found no answer.. hope you can help me with this. sample: text... (2 Replies)
Discussion started by: paoie
2 Replies

7. Shell Programming and Scripting

Perl & Sed command -- Out of Memory Error

Experts, We used to receive our source files with '~^' as row delimiter. This file contains 2500K records and two of the columns having value in HTML formats within the file. While running the below commands against the file, we are encountering out of memory, could you please help to... (3 Replies)
Discussion started by: srivijay81
3 Replies

8. Shell Programming and Scripting

What's wrong with this sed command? delete & append

I want to write a sed command that does the following work: file: <a>asdfasdf<\s> <line>hello</line> <b>adf<\c> <b>tttttttt<\c> output: name=hello sed -e 's/^*//' -n -e '/<line>/s/<*>//gp;' -e 's/^/name="/g' file but I can not append "=" after getting the line with... (5 Replies)
Discussion started by: minifish
5 Replies

9. Shell Programming and Scripting

Issues with cut

We are running on a Sun cluster using ksh. We have a script that has been running for about six months with no issues. The script has not been modified and our results are now coming out diffrent. <BEGIN> cat $exfile | while read line do ... (1 Reply)
Discussion started by: bthomas
1 Replies
Login or Register to Ask a Question