0

I have a filename variable that sometimes gets set as:

attachment; 
JobRunStatisticsReport_20150122.zip

In the File Manager window, it looks like this:

enter image description here

I need help coming up with an IF statement that says:

IF $filename contains 'attachment; \n' then

and then help with a SED that looks for that new line character (\n) and replaces that and everything before it with nothing.

Can anyone lend a quick hand please?

4
  • 1
    Why sed specifically? it doesn't naturally deal with multi-line input, and if you already have the name in a shell variable why not try something bash-based like "${var#*$'\n'}"? Jan 30, 2015 at 19:47
  • I suppose you tried sed 's/.*\\n//g' and sed 's/.*\\r//g' ? Jan 30, 2015 at 19:51
  • it has something to do with characters that are not seen by my terminal window when I just echo $filename . If I type this: > for k in $(ls "$dec"); do > echo $k > done > >attachment; >JobRunStatisticsReport_20150122.zip > But if I just echo $filename, I get: > echo $filename > JobRunStatisticsReport_20150122.zip I know it doesn't copy out well here, but just echoing the value gives the correct name. But when my script hit it, it grabs the attachment; part of it. Jan 30, 2015 at 20:07
  • Perhaps you should post the relevant portion of your script, as it appears you are doing some questionable things: to start, don't use for k in $(ls "$dec") as it will word split - use for k in "$dec"/* instead, and remember to also quote k when you use it "$k". Jan 30, 2015 at 20:22

1 Answer 1

2

Your filename actually contains both a carriage return and a newline. bash parameter expansion can handle this:

$ filename=$'attachment;\r\nJobRunStatisticsReport_20150122.zip'
$ echo "$filename"
attachment;
JobRunStatisticsReport_20150122.zip
$ shopt -s extglob
$ echo "${filename##attachment;*([[:space:]])}"
JobRunStatisticsReport_20150122.zip

Does not affect "normal" filenames:

$ filename="no attachment here"
$ echo "${filename##attachment;*([[:space:]])}"
no attachment here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .