1

Please help me in solving the below issue. I have a file:

mat rat
mat dog
mat matress

I need to display

rat
dog
matress

I have coded with sed command to display the output: sed "s/$up//g" ($up will contain mat) . But using this command, I am getting the output as

rat
dog
ress

What do I do to resolve this?.
Please help.

Birei
  • 35,723
  • 2
  • 77
  • 82
  • Although the possible duplicate (SO 1032023) could be used to solve this, it is taking a version-dependent sledgehammer to crack a simple nut. In this case, the correct solution is to drop the `g` suffix so the code only drops the leading `mat`. There's also a question whether the pattern to be excised should be `s/^mat //` with the trailing space; that won't affect the `matress` (or `mattress`). – Jonathan Leffler Mar 15 '14 at 05:18

4 Answers4

0

The /g flag tries to apply the substitution command multiple times for each line. First two lines are fine because the word only appears once, but for the third line it will remove both.

You can solve it being more specific using zero-width assertions, like ^, or the GNU extension \b, like:

sed "s/^$up//g"

or

sed "s/$up\b//g"

Although the easier could be to remove the flag, like:

sed "s/$up//"

In all three cases the result is the same, at least for this kind of simple examples.

Birei
  • 35,723
  • 2
  • 77
  • 82
  • It does not work .When i executed the above statement , the word mat is also getting appended along with the output.I need to remove the mat and display only the rat dog matress –  Jun 26 '13 at 13:11
0

Using awk

awk '{print $NF}' inputFile

Test:

$ cat text
mat rat
mat dog
mat matress

$ awk '{print $NF}' text
rat
dog
matress
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • This does not work.It gives me only one item as ouput –  Jun 26 '13 at 13:13
  • @user2519862 can you be more specific about the issue? I have added a sample test of how it shows what you need? – jaypal singh Jun 26 '13 at 13:24
  • I have a1.dat file wherein the contents of the file are abc def abc ghi abc abcjkl I used sed command (sed "s/$up//g" a1.dat >a1.out) ... here $up will conatin "abc" .That is I am reading $up first and then using sed command to replace all the abc with nothng .. So as to get the output as (def ghi abcjkl) . But now I am getting (def ghi jkl) as output. Ideally i need the output as (def ghi abcjkl). pls help –  Jun 26 '13 at 14:11
  • remove `g` tag from your `sed` command. That would prevent it from making more than one change on single line. – jaypal singh Jun 26 '13 at 14:19
  • If i remove g, then it will replace only the first line . Ideally I want all the ocurrences of mat to get changed , (only in the first column and not in the second column) . The concept is I need to replace the occurence of mat with blank space only for the first column and the second column datas should get displayed as such. –  Jun 27 '13 at 08:17
0

Your current command will remove all instances of $up anywhere, including multiple occurrences in a line and occurrences in the middle of a line.

If you want to match only $up at the very beginning of a line, and only when it is a whole (whitespace-delimited) word, try the following command:

sed "s/^$up\>//"

In GNU sed, the assertion ^ matches to the beginning of a line, and \> matches the end of a word (the zero-width "character" between a non-whitespace character and whitespace character).

If there might be whitespace before $up, you can use

sed "s/\(\s*\)$up\>/\1/"

This will remove just the $up and preserve all whitespace.

If you don't want to keep the whitespace between $up and the text after it, you can replace \> with \s\+, which matches to one or more (\+) whitespace characters (\s); i.e.,

sed "s/^$up\s\+//"
sed "s/\(\s*\)$up\s\+/\1/"
Bitz
  • 49
  • 3
0

sed 's/^mat //' /path/to/file should do the trick. Note that there is no g; it's s/foo/bar; not s/foo/bar/g. Also, the ^ pegs the replacement to the beginning of each line.

If you are indeed assigning a variable such as $up, you can use sed "s/^$up//" /path/to/file.

DopeGhoti
  • 757
  • 7
  • 15