0

I have this file I want to add a line with and I seem to have a problem when using alt characters. Here's the script piece:

#testfile.txt
Main
├────►projectA
└────►projectC
sed "/├────►projectA/a ├────►projectB/" testfile.txt

sed doesn't seem to find the "├────►projectA" portion. Grep won't even find it.

grep "├────►projectA" testfile.txt

grep returns nothing.

So how can you make it found so I can add my line below it?

Edit: I found my problem. I was using the wrong character in my sed command. This script is on a different system so I had to make an example off the top of my head.

I'm trying to add spaces as well after the /a but it trims it. Is there a way to preserve the spaces?
(e.g. "[5 spaces] ├────►projectB")
I can't add spaces to the above line because stakoverflow formatting trims it as well. So I say [5 spaces] to represent the amount of whitespace.

Gene
  • 97
  • 6
  • 1
    I suspect you have different encodings in your terminal and different in your file or maybe there are other unprintable characters in your file that are missing from terminal. Please post the output of like`hexdump -C testfile.txt` and the output of `echo "├────►projectA" | hexdump -C` and will compare the bytes. Note: the `a` command in `sed` does not end with `/`. For example if I copy&paste the code you presented, `sed` and `grep` works for me. How is the `testfile.txt` generated? – KamilCuk Feb 07 '20 at 17:32
  • Depending on how picky you want to be, a kludge might be to `sed "/projectA/a ├────►projectB/"` though I prefer to be more precise when I can. – Paul Hodges Feb 07 '20 at 17:43
  • 1
    Works as you wrote it for me, though it adds `/` at the end which isn't on A and C lines. – Paul Hodges Feb 07 '20 at 17:44
  • Yes, I noticed if I don't use the alt characters, this works as expected. And yes, i noticed I get the slash at the end too. I'm convinced it's the alt characters causing the problem. When I don't have it in the first part (with projectA), it works. – Gene Feb 07 '20 at 17:58
  • I see my problem. I used the wrong character. – Gene Feb 07 '20 at 18:03
  • On a separate note, I'm trying to add spaces after the /a part of the sed command, but it reduces it to no spaces. how can I add spaces? – Gene Feb 07 '20 at 18:04

2 Answers2

0

It seems to me like you're trying to use sed to append a literal string (i,.e. a string containing any characters) but sed doesn't understand literal strings, only regular expressions and backreference-enabled replacements, see Is it possible to escape regex metacharacters reliably with sed. You should instead be using a tool like awk that understands literal strings.

Is this all you want to do?

$ awk '{print} index($0,"├────►projectA"){print "├────►projectB"}' file
#testfile.txt
Main
├────►projectA
├────►projectB
└────►projectC

If you want 5 leading blanks, just add 5 leading blanks to the string in the print statement:

$ awk '{print} index($0,"├────►projectA"){print "     ├────►projectB"}' file
#testfile.txt
Main
├────►projectA
     ├────►projectB
└────►projectC

If you just want to duplicate whatever indent the preceding line has, here's a modified version of your input file:

$ cat file
#testfile.txt
   Main
     ├────►projectA
     └────►projectC

and here's how to print the new line with whatever indent (blanks and/or tabs) the preceding line uses:

$ awk '{print} s=index($0,"├────►projectA"){print substr($0,1,s-1) "├────►projectB"}' file
#testfile.txt
   Main
     ├────►projectA
     ├────►projectB
     └────►projectC
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

This might work for you (GNU sed):

sed '/^├────►projectA$/{p;s/A/B/}' file

Match on ────►projectA, print the line then substitute B for A.

Use the sed command l0 to see each lines representation in octal and ascii.

Thus this is the same as above:

sed '/^\o342\o224\o234\o342\o224\o200\o342\o224\o200\o342\o224\o200\o342\o224\o200\o342\o226\o272projectA$/{p;s/A/B/}' file

To add 5 spaces to front of such a line, use:

sed '/^├────►projectA$/{p;s/^/     /;s/A/B/}' file

Or you may prefer:

sed '/^\(├────►project\)A$/{p;s/     \1B/}' file
potong
  • 55,640
  • 6
  • 51
  • 83