How do I check if string starts with '#' ?
#path /var/dumpfolder
I tried awk for
awk -F '' '{print $1}'
Is there any better way ?
How do I check if string starts with '#' ?
#path /var/dumpfolder
I tried awk for
awk -F '' '{print $1}'
Is there any better way ?
I still cannot understand what are you trying to do, but there is bash 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.
If sometimes there are leading spaces, then use regexp:
if [[ $string =~ ^[[:space:]]*\#.*$ ]]; then
echo "${string#*\#}"
else
echo "$string"
fi
^\s*[#](.*)$
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 stringThis should work to remove the #'s in your code and spaces in front of the comment
sed 's/\^\s*[#](.*)$/\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
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