LinuxQuestions.org
Review your favorite Linux distribution.
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

Reply
 
Search this Thread
Old 10-11-2012, 12:27 AM   #1
Linuxnoobie123
LQ Newbie
 
Registered: Oct 2012
Posts: 7

Rep: Reputation: Disabled
Linux command help


Hi there
I'm new to this forum:
I was wondering if someone could help me figure out a task

In the second field, change 'f' to female and 'm' to male globally. Display the first 9 lines.

Use 2 sed statements one piped to another.
Note use of single quotes in examples.

This was my output

[sjim7@hills ~]$ grep -k2 |sed 's/m/male/g' famous.data | sed 's/f/female/g' | head -n 9
grep: invalid option -- 'k'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
01 male maleotzart amaleadeous 25 2nd 94233
02 male guthrie woody 23 2nd 94223
03 female simaleone nina 27 2nd 94112
04 male lennon john 29 2nd 94221
05 female harris emalemaleylou 20 2nd 94222
06 male malearley bob 22 2nd 94112
07 female malearley rita 26 2nd 94212
08 female warwick dione 26 2nd 94222
09 male prine john 35 3rd 94321

How would i type it to focus on the second column to change the m to males, and f to females , instead of changing every letter in each column that has a m to male , thanks
 
Old 10-11-2012, 12:54 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,734

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
duplicate of https://www.linuxquestions.org/quest...em-4175431599/
 
Old 10-11-2012, 01:04 AM   #3
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,970

Rep: Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848
Hi, you should post the sourcedata as well. Probably it looked like that:
Code:
01 m motzart amadeous 25 2nd 94233
02 m guthrie woody 23 2nd 94223
03 f simone nina 27 2nd 94112
04 m lennon john 29 2nd 94221
05 f harris emmylou 20 2nd 94222
06 m marley bob 22 2nd 94112
07 f marley rita 26 2nd 94212
08 f warwick dione 26 2nd 94222
09 m prine john 35 3rd 94321
But your problem is that you search for any f and any m, but you'll have to search only for single letters f and m. But in your case it seems easier, don't use the g option since it substitutes any f or m on a line, without g it substitutes only the first occurence of the letter.
Code:
sed -n s/f/female/ p
should work for you.

Markus
 
Old 10-11-2012, 01:16 AM   #4
Linuxnoobie123
LQ Newbie
 
Registered: Oct 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hey Markus,
Sorry, but i tried replacing that command that you stated above, it doesn't work
Do you mind copying and pasting the command below , thanks

[sjim7@hills ~]$ grep -k2 |sed 's/m/male/g' famous.data | sed 's/f/female/g' | head -n 9
 
Old 10-11-2012, 05:35 AM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,970

Rep: Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848Reputation: 848
Well, when I put the data into a file data.txt with the content
Code:
01 m motzart amadeous 25 2nd 94233
02 m guthrie woody 23 2nd 94223
03 f simone nina 27 2nd 94112                                                     
04 m lennon john 29 2nd 94221                                                     
05 f harris emmylou 20 2nd 94222                                                  
06 m marley bob 22 2nd 94112                                                      
07 f marley rita 26 2nd 94212                                                     
08 f warwick dione 26 2nd 94222                                                   
09 m prine john 35 3rd 94321
I get this output:
Code:
markus@samsung:~$ sed -n 's/f/female/ p' < data.txt 
03 female simone nina 27 2nd 94112
05 female harris emmylou 20 2nd 94222
07 female marley rita 26 2nd 94212
08 female warwick dione 26 2nd 94222
If you want more help, please post the file with your complete data to help us trying it out.

You should try
Code:
grep -k2 |sed -n 's/m/male/ p' famous.data | sed -n 's/f/female/ p' | head -n 9
Markus

Last edited by markush; 10-11-2012 at 05:37 AM.
 
Old 10-12-2012, 01:46 PM   #6
overlook
LQ Newbie
 
Registered: May 2006
Location: Sweden
Distribution: Custom
Posts: 11

Rep: Reputation: 1
I'm a bit tired so I can't think of a better solution atm. but this script should do what you want. I'm sure there are better, more elegant, ways though.
Code:
#!/bin/bash

if [ -z $1 ]; then
  echo "syntax: $0 <filename>"
  exit 1
fi
IFS=" "
while read f1 f2 f3 f4 f5 f6 f7 ; do
 if [ $f2 = "m" ]; then
  f2=male
 elif [ $f2 = "f" ]; then
  f2=female
 fi
 echo $f1 $f2 $f3 $f4 $f5 $f6 $f7
done < $1
Used as such:
Code:
script.sh famous.data
 
Old 10-13-2012, 11:42 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Markush's sed expression should be fine. If you would post the actual command you used and what you actually got in return instead of just saying "it didn't work", we might be able to figure out why.

In any case, one big problem you have is the first part of your command chain:

Code:
grep -k2 |sed 's/m/male/g' famous.data
sed can take input from either a file, or from a pipe, but not both at once. And your grep command doesn't have any input, nor does it have a "-k" option. Could you explain what you're trying to do here? I imagine the rest of it will work if you leave the grep out.

Finally, since your assignment says to use two sed commands you have to do it that way, but actually the whole thing, including the function of head, can be completed with a single, relatively easy sed command (a single two-part address and two sub-expressions).

I'll leave it up to you to figure out the details.

Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How does Linux cp command determine the source and destination locations in command? linuxman2 Linux - General 4 04-14-2011 07:13 PM
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 02:14 AM
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 06:30 PM
Translating windows pscp command to linux scp command help robward Linux - General 2 01-17-2008 06:02 AM
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM


All times are GMT -5. The time now is 11:02 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration