3

I'm not sure how to express this using the csh string matching syntax. I want to test whether a csh variable contains a newline. I'm basically looking for:

if ($mystr !~ <pattern for strings which contain a newline character>)

Edit: in my particular case, I am trying to make a string like this pass:

1234ABC

And a string like this fail:

1234ABC
 -------
FOOBAR

These are the output of a sed command, namely sed '1d;$d'. Not sure if that matters.

The reason why I am trying to detect newlines rather than " -------" is for defense against changes in the formatting of the file I'm parsing. (Anyway, I don't think it matters what I'm doing with the file exactly, since I'm just looking for a general solution for detecting a newline character.)

Mikel
  • 57,299
  • 15
  • 134
  • 153
2rs2ts
  • 163
  • Does it need to be csh? Do you want to check if the string is contained in $mystr or if the two are identical? – terdon May 07 '14 at 18:05
  • @terdon Yes it does need to be csh, and I just want to check to see if there is a newline character in $mystr. – 2rs2ts May 07 '14 at 18:12
  • What? Do you want to check whether $mystr is/contains a specific multiline string or do you just need to check whether $mystr contains newline character(s)? The two are completely different. Please [edit] your question and clarify. – terdon May 07 '14 at 18:14
  • @terdon There, does that help? – 2rs2ts May 07 '14 at 18:23
  • I edited with what I think you mean. I beleive all you're looking for is the newline character, \n but have no csh knowledge so I don't know what the match syntax would be or even if csh can match. – terdon May 07 '14 at 18:26
  • Was it really necessary to reiterate the title? – 2rs2ts May 07 '14 at 18:26
  • Well, it was for me, yes. That's why I wasn't understanding. The syntax you show implies that you are matching $mystr against a specific multiline variable. I edited to clarify that's not what you're after. Feel free to revert. – terdon May 07 '14 at 18:26

1 Answers1

5
if ($mystr:q =~ *'\
'*) echo yes

should work in some implementations and versions of csh (like the csh and tcsh ones found on Debian). In some others (like the one found on Solaris 10), you may have better luck with

set nl = '\
'
if ($mystr:q =~ *$nl:q*) echo yes

Most people have given up trying to write reliable scripts with csh by now. Why would you use csh in this century?

This code works for me (outputs no) in tcsh 6.17.00 (Astron) 2009-07-10 (x86_64-unknown-linux) options wide,nls,dl,al,kan,rh,color,filec

set mystr = '1234ABC\
 -------\
FOOBAR'
if ($mystr:q !~ *'\
'*) then
  echo yes
else
  echo no
endif

Note that if you do:

set var = `some command`

csh stores each word (blank separated) of the output of some command in several elements of the var array.

With:

set var = "`some command`"

it stores each non-empty line in elements of the array.

It looks like one cannot1 store the output of a command whole into a variable in (t)csh, so your only option would be:

set var = "`some command`" # note that it removes the empty lines
if ($#var == 1)...

1 Strictly speaking, that's not true, one could do something like:

set x = "`some command | paste -d. /dev/null -`"
set var = ""
set nl = '\
'

foreach i ($x:q)
  set i = $i:s/.//:q
  set var = $var:q$i:q$nl:q
end

(of course, it may not work in all csh implementations/versions)

  • When I ps x I get -csh. Does that mean I'm not running tcsh, or does that not tell me anything? – 2rs2ts May 07 '14 at 19:13
  • 1
    Sorry, my bad, $var:q was already available in csh. I would still advise against using (t)csh for scripting though. – Stéphane Chazelas May 07 '14 at 19:17
  • No idea why you took the disclaimer out of your answer, it was actually pretty good advice. – Bratchley May 07 '14 at 19:23
  • I'm getting if: Badly formed number. I'm using it like so: if ($mystr:q !~ *'\ '*) then. Mind you can't see the newline... – 2rs2ts May 07 '14 at 19:24
  • @2rs2ts, try with the updated answer. – Stéphane Chazelas May 07 '14 at 19:45
  • Still no luck. This is what the version seems to be: tcsh 6.17.00 (Astron) 2009-07-10 (x86_64-unknown-linux) options wide,nls,dl,al,kan,rh,color,filec (CentOS 6 here.) The reason why I'm doing it in csh is because I'm editing legacy csh code. – 2rs2ts May 07 '14 at 19:50
  • @2rs2ts Both work for me with that same version of tcsh. Does that work if you put that code by itself in a file and run it? – Stéphane Chazelas May 07 '14 at 20:25
  • Hmm, yes it does. In truth $mystr is the result of a sed '1d;$d' call in my script (i.e. set mystr=\cat somefile.txt | sed '1d;$d'``.) I don't know, could that be related to the problem? I was able to successfully check if $mystr != "". – 2rs2ts May 07 '14 at 20:38
  • @2rs2ts, I can't tell as I can't reproduce. Possibly the variable contains a value that triggers a bug. It looks somehow, tcsh is trying to interpret something as a number and fails to parse it. (like if (09 > 1) would cause that same error message). – Stéphane Chazelas May 07 '14 at 21:27
  • @StephaneChazelas The string in question starts with a numeral, but it contains whitespace, letters, and special characters. So can I do anything about this? – 2rs2ts May 08 '14 at 12:07
  • @2rs2ts, can you post a sample string that exhibits the problem? Otherwise you could try if (x$mystr:q !~... to see if that helps. – Stéphane Chazelas May 08 '14 at 12:13
  • @StephaneChazelas I got if: Expression Syntax. I will edit my question with an example string. – 2rs2ts May 08 '14 at 12:15
  • OK, I get it, your variable is an array. – Stéphane Chazelas May 08 '14 at 12:33
  • @StephaneChazelas Thanks, but I don't understand the purpose of set var = "\some command`"followed byset myvar = "`some command`"`. Seems to me that they're exactly the same...? – 2rs2ts May 08 '14 at 12:54
  • Electrical Engineers predominantly use csh/tcsh because of the EDA and CAD software still using csh scripts. The comments about 'don't use csh' get old, and come from people unaware of valid reasons and unimaginative enough to think of any – onlinespending Feb 12 '22 at 17:48