LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

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

Rep: Reputation: Disabled
Linux command problem


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:49 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,436
Blog Entries: 11

Rep: Reputation: 181Reputation: 181
Could you tell me the reason why you posted this three times?
http://www.linuxquestions.org/questi...lp-4175431592/
http://www.linuxquestions.org/questi...ng-4175431591/

Think about how the m that you want to substitute with male differs from all the other m's in a line. Think about what seperates words.
 
Old 10-11-2012, 12:53 AM   #3
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,466

Rep: Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213
grep -k2 at the beginning has no effect at all, and also it does not work (grep: invalid option -- 'k')
You would also need to understand the g at the and of the sed commands, probably that should be changed. Also you can use only one sed command instead of two invocations. You can try awk also, if it was more convenient for you.
 
Old 10-11-2012, 12:58 AM   #4
Linuxnoobie123
LQ Newbie
 
Registered: Oct 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
grep -k2 at the beginning has no effect at all, and also it does not work (grep: invalid option -- 'k')
You would also need to understand the g at the and of the sed commands, probably that should be changed. Also you can use only one sed command instead of two invocations. You can try awk also, if it was more convenient for you.
would you be able to do it in awk ?
im not sure how to do it in awk, i was trying to see if i could use a sort function on the command line im currently using to only change the 2nd column m to males. To change all the m to male and f to female, without having all the other m's in different columns switch to male too

---------- Post added 10-11-12 at 12:59 AM ----------

Quote:
Originally Posted by zhjim View Post
Could you tell me the reason why you posted this three times?
http://www.linuxquestions.org/questi...lp-4175431592/
http://www.linuxquestions.org/questi...ng-4175431591/

Think about how the m that you want to substitute with male differs from all the other m's in a line. Think about what seperates words.
the thing that separate words ??
 
Old 10-11-2012, 01:02 AM   #5
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,466

Rep: Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213
Yes, I can do it using awk, perl, sed, c or probably only bash, that is not a problem. But I would rather see you can do it yourself.
Can you explain how did you find the original command you tried?
 
Old 10-11-2012, 01:07 AM   #6
Linuxnoobie123
LQ Newbie
 
Registered: Oct 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hey Pan, i just typed out the command from the notes i had

[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
 
Old 10-11-2012, 01:10 AM   #7
Linuxnoobie123
LQ Newbie
 
Registered: Oct 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
I'm trying to only search for the m/f so i can change the m to male and f to female in column 2 only
Can you please help me, I've been trying to figure this out for 2 days already !!

and this is the output i get

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

its suppose to be this :
01 male motzart amadeous 25 2nd 94233
02 male guthrie woody 23 2nd 94223
03 female simone nina 27 2nd 94112
04 male lennon john 29 2nd 94221
05 female harris emmylou 20 2nd 94222
06 male marley bob 22 2nd 94112
07 female marley rita 26 2nd 94212
08 female warwick dione 26 2nd 94222
09 male prine john 35 3rd 94321
 
Old 10-11-2012, 01:44 AM   #8
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,436
Blog Entries: 11

Rep: Reputation: 181Reputation: 181
Quote:
Originally Posted by Linuxnoobie123 View Post
the thing that separate words ??
I most of the time refrain answering peoples questions when they don't answer mine as well. As I do now and continue to enjoy my space.
 
1 members found this post helpful.
Old 10-11-2012, 03:40 AM   #9
henrycoffin
Member
 
Registered: Dec 2006
Distribution: RHEL Debian
Posts: 42

Rep: Reputation: 15
Just match the lettr m followed by whitespace

sed 's/m\s/male/g' or sed 's/m /male /g'
 
Old 10-11-2012, 07:17 AM   #10
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,024
Blog Entries: 1

Rep: Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364
Moderator Response

Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate of;
http://www.linuxquestions.org/questi...lp-4175431592/
http://www.linuxquestions.org/questi...ng-4175431591/

You should stay with the original: http://www.linuxquestions.org/questi...ng-4175431591/
 
  


Closed Thread


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linux command problem... atul1958 Linux - General 8 01-30-2010 03:58 AM
Linux command line problem mickeyboa Fedora 3 03-20-2007 07:42 PM
problem in rsh command in linux gopi_raghu Linux - Software 4 01-19-2007 03:22 AM
Simple Linux command Problem GUIPenguin Linux - Newbie 2 09-23-2004 04:47 PM
weird command problem in linux sonesay Linux - Newbie 9 05-21-2004 07:07 AM


All times are GMT -5. The time now is 09:58 PM.

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