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 |
|
Hi, you should post the sourcedata as well. Probably it looked like that:
Code:
01 m motzart amadeous 25 2nd 94233 Code:
sed -n s/f/female/ p Markus |
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 |
Well, when I put the data into a file data.txt with the content
Code:
01 m motzart amadeous 25 2nd 94233 Code:
markus@samsung:~$ sed -n 's/f/female/ p' < data.txt You should try Code:
grep -k2 |sed -n 's/m/male/ p' famous.data | sed -n 's/f/female/ p' | head -n 9 |
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 Code:
script.sh famous.data |
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 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 |
All times are GMT -5. The time now is 09:48 AM. |