I am dealing with this for some time now and without success. I need to sed this


into this


Any help would be appriciated.


First line respresents string inside of a file. Second line - result is going to be used in CSV file.

This part contains fixed number of characters (always): 1.0.0/hk/base/ This part can vary with a number of characters: 9/7/56 2003444

I was told to do it like this:

-e 's/\/base\//,/g'

which is going to return:


But I have no idea how to deal with other stuff.

  • That's easy to do by simply deleting characters using .. But I hardly believe that is what you want. I believe there is some pattern (i.e. naming convention) of those directories. If you provide that then people can give you the answer you're looking for. (Otherwise you will get an answer that is correct for this specific input, but unlikely doing what you want). – grochmal Oct 6 '16 at 18:24
  • I want exectly this. I need to make csv from the given lines. And the pattern is same in all lines (hopefully). I have no idea how to delete it with . Can you provide me some answer please. – newbie_girl Oct 6 '16 at 18:27
  • Is this a string or a directory path? If the latter, will it always be a directory path? Will it always be exactly this many levels deep? Can you describe the problem you're trying to solve by using this transformation? – John Oct 6 '16 at 18:27
  • its a string inside of a file. Trying to make csv file. – newbie_girl Oct 6 '16 at 18:27
  • Since it's delimited, you may find that the solution is more readable in awk e.g. awk -F/ '{print $2,$4,$5,substr($6,1,2),substr($6,7,3),substr($6,10)}' OFS=, – steeldriver Oct 6 '16 at 18:38

For this string, this transform should work:

sed 's|.*/\(.*\)/.*/\(.*\)/\(.*\)/\(.*\)\.png\(...\)\(....\)|\1,\2,\3,\4,\5\,\6|'

If other lines are not exactly the format, you will run into issues - including if the last two parts are different lengths.

  • Hi! Sorry for not specifying formating. Didnt understanf thats going to be an issue. So this doesnt work. Will edit my question again. – newbie_girl Oct 6 '16 at 18:37

Since it is a file I'd do it in Vim. Vim's very magic regexes are very readable and, therefore, good educational source:

  • \v turn very magic on
  • ^ start match at the start of the line
  • .{6} get the first 6 characters (1.0.0/)
  • ([^/]+) anything that is not / (hk)
  • / the /
  • ([^/]+) we saw that before (matches base this time)
  • / -- || -- || --
  • (\d+) matches numbers (9)
  • /(\d+)/(\d+) repeat the precious stuff (/7/56)
  • \.png this should be obvious
  • (\d{3}) exactly three numbers 200
  • (\d{,4}) up to 4 numbers (to make this more robust): 3444

(To use with sed every {}()[] needs to be escaped and \v removed)


You haven't told us about the structure of the text you are parsing, or the pattern for which you want to search. So, I have made a few assumptions based on the example that you gave us (1.0.0/hk/base/9/7/56.png2003444).

I have used extended regular expressions (-r), since escaping parentheses and brackets makes the script hard to read. And I have used semicolons (;) instead of slash (/) to separate the command, so that I wouldn't need to escape slashes either.

sed -r 's;([0-9]+\.){2}[0-9]+/([a-zA-Z]+)/[a-zA-Z]+/([0-9]+)/([0-9]+)/([0-9]+)\.[a-zA-Z]{3}([0-9]{3})([0-9]{4});\2,\3,\4,\5,\6,\7;g'


Using GNU sed, and supposing the strings 'base' and 'png' are unwanted constants, plus a few other broad assumptions:

echo '1.0.0/hk/base/9/7/56.png2003444' | \
sed 's#[^/]*/##;s#base\|png##g;s#//#/#;y#/.#,,#;s/....$/,&/'



So far this is the only answer to used sed's y function, which works much like Unix's tr tool.

  • Coincidentally, (using the OP's own answer as a spec), all of the above assumptions appear to have been correct. – agc Oct 7 '16 at 2:20

Thanks for all the replies. I am new in the world of programming and this is also a new thing for me, so I guess I need to learn a lot, since sed is an old school method of dealing with this stuff. However as I was following certain approach I was kind of familiar with, I ended up solving the problem like this:

sed -e 's/1.0.0\///g'     \
    -e 's/\/base\//,/g'   \
    -e 's/\//,/g'         \
    -e 's/.png/,/g'       \
    -e 's/,\([0-9][0-9][0-9]\)\([0-9]*\)$/,\1,\2/g'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.