0

How do I check if string starts with '#' ?

#path /var/dumpfolder 

I tried awk for

awk -F '' '{print $1}'

Is there any better way ?

fujy
  • 5,168
  • 5
  • 31
  • 50
run_time_rookie
  • 69
  • 1
  • 1
  • 7

5 Answers5

3

I still cannot understand what are you trying to do, but there is in your tags, so here is a bash solution to check if string starts with '#'

string='#path /var/dumpfolder'

if [[ $string == \#* ]]; then
    echo 'yes'
else
    echo 'no'
fi

or:

if [[ ${string:0:1} == '#' ]]; then
    echo 'yes'
else
    echo 'no'
fi

If you want to remove # character then use:

echo "${string#\#}" # prints 'path /var/dumpfolder'

This will remove # character if it exists. If it doesn't exist then the string will be left intact.

Edit:

If sometimes there are leading spaces, then use regexp:

if [[ $string =~ ^[[:space:]]*\#.*$ ]]; then
    echo "${string#*\#}"
else
    echo "$string"
fi
Aleks-Daniel Jakimenko-A.
  • 10,335
  • 3
  • 41
  • 39
2
^\s*[#](.*)$

Regular expression visualization

Edit live on Debuggex

here is a regular expression ..... if that's what you are looking for.

  • ^ denotes the start of the string
  • \s* means a one more spaces to take care of the case where there could be a space in front of the #
  • [#] or just # works this checks that the first letter is a #
  • (.*) grabs all(most) of the characters and throws them into a capture group
  • $ stops at the end of the string

This should work to remove the #'s in your code and spaces in front of the comment

sed 's/\^\s*[#](.*)$/\1/'
Community
  • 1
  • 1
progrenhard
  • 2,333
  • 2
  • 14
  • 14
  • I just need to check if commented if so remove comment and extract rest of string. The regex gives me complete string with even '#' included and I want only rest of string – run_time_rookie Sep 05 '13 at 22:26
  • 1
    Why ``#`` in ``[]`` ? Seems like there's no need. Also why ``.*`` is in ``( )`` ? ``^\s#.*$`` should work as well. – Aleks-Daniel Jakimenko-A. Sep 05 '13 at 22:36
  • @Aleks-DanielJakimenko For clarification you're right! I could leave it out. Also about the .*, I like putting stuff I want to match into capture groups just In case later i want to use them for something other than matching.(putting the values into an array) – progrenhard Sep 05 '13 at 22:38
1

If you just want to find #path or path (without #) pattern in file, do:

grep -E '^#?path' file

To just find #path exactly, don't add ?:

grep -E '^#path' file

More exlicit is to add a space, but your separator could be a tab as well:

grep -E '^#path ' file
konsolebox
  • 72,135
  • 12
  • 99
  • 105
1

I basically want to check if string is commented if so, I want to remove that comment '#' from string and keep it. Yes a message or storing string without the comment.

so

sed 's/^\s*#*//' 

example (with file):

kent$  cat f
#c1
 #c2
foo
bar

kent$  sed 's/^\s*#*//' f
c1
c2
foo
bar
Kent
  • 189,393
  • 32
  • 233
  • 301
0

Using awk

awk '{sub(/^#/,"")}1' file
Jotne
  • 40,548
  • 12
  • 51
  • 55