LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 10-04-2012, 09:56 PM   #1
r_clark2
LQ Newbie
 
Registered: Oct 2012
Location: MD
Distribution: Unix 12.04 LTS
Posts: 3

Rep: Reputation: Disabled
Unhappy Need help writing a simple sed script


I have to write a single sed script that will remove items in a text file that was assigned to work with. The issue that I have is I have to delete just the salaries in a line that end with 500, the salaries are at the end of the lines and again I just need to delete the salaries that end in 500. Please help!
Here is what the text looks like:
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700
James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500
Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500
Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000
Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350
Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600
Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000
Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900

And again I have to write a single sed script and cat the script in the text to make the changes and I have an even bigger challenge after this. I have to change the names to be last, first name and the currently are listed as first, last name.
I have attempted several scripts already and I have yet to get it to work, such as /...500$// /...500$d/
 
Old 10-04-2012, 11:25 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Try this...
Code:
sed 's/ /\:/' $InFile \
|awk -F ":" '{print $2", "$1":"$3":"$4":"$5":"$6}' \
|rev \
|awk -F ":"  \
  '{if (substr($1,1,3)=="005") \
    print $2":"$3":"$4":"$5;   \
    else print $0}'            \
|rev
This is kinda-sorta brute force.
It assumes that all names are two words.
No middle names, no middle initials, no names which contain blanks such as "Kevin Le Duc" or "Jose de la Moneda".

Daniel B. Martin
 
Old 10-05-2012, 01:52 AM   #3
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
EDIT:

How about?? ->

Code:
cat test.txt | awk '{ print $0":"$0":"$0}' | sed '/500$/d' | cut -d: -f5,7,8,9,11

Last edited by amboxer21; 10-05-2012 at 03:53 AM.
 
Old 10-05-2012, 03:35 AM   #4
ip_address
Member
 
Registered: Apr 2012
Distribution: RedHat
Posts: 42

Rep: Reputation: 2
you can try this:

Code:
awk -F":" < salaries.txt 'OFS=":" {if ($NF~"500$") {$NF="";print} else {print}}'

Last edited by ip_address; 10-05-2012 at 03:36 AM.
 
Old 10-05-2012, 03:46 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@r_clark2: Please don't cross-post, it is against the LQ rules!

Other thread: https://www.linuxquestions.org/quest...pt-4175430591/

Reported.
 
Old 10-05-2012, 03:48 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,927

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
you should not write such chains like: cat test.txt | awk '{ if(NF == 7) print $7":"$0":"$0}' | sed '/500$/d' | cut -d: -f3,5,6,7,9
instead:
awk -F: ' /500$/ { next } { split($1, a, " "); $1 = a[2] " " a[1]; print } ' inputfile
 
1 members found this post helpful.
Old 10-05-2012, 03:58 AM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.

Everyone else... Why have you helped someone cheat?

Last edited by acid_kewpie; 10-05-2012 at 04:14 AM.
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing a simple script... imprise Linux - Newbie 3 05-21-2009 03:08 AM
Need help writing a simple bash script linuxmaveric Programming 3 12-06-2007 06:53 PM
Writing shell script with sed,sql, and epoch milliseconds Eamo Programming 7 07-16-2007 03:40 PM
Help writing a simple script Garibaldi3489 Linux - General 10 12-18-2004 06:22 PM
Simple script writing help - digital camera EuroJovi Linux - General 2 02-01-2004 04:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration