LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help on grep command (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-on-grep-command-775684/)

wjs1990 12-14-2009 10:27 PM

Need help on grep command
 
Hi all,
For example, i have two similar line "net.ipv4.tcp_syncookies = 1" and "net.ipv4.tcp_syncookies =1". The difference is just that one of them have space between the "=" and "1", and the other one does not have.
Then i used, the command " grep -v "^#" /etc/sysctl.conf | grep -w net.ipv4.tcp_syncookies | awk -F= '{print $2}' ". The results i get will be " 1" and "1" for the two lines respectively.
So, does anyone have any idea on how can i get the result "1" for both lines, using the same command.

Thanks

ghostdog74 12-14-2009 10:35 PM

just one awk command will do
Code:

awk -F'= ' '!/^#/&&/net.*ipv4.*tcp_syncookies/{print $2}' /etc/sysctl.conf
or using the shell
Code:

while read -r line
do
    case "$line" in
      net.ipv4.tcp_syncookies*)
      IFS="="
      set -- $line
      echo ${2// }
    esac
done <"/etc/sysctl.conf"


syg00 12-14-2009 10:59 PM

Code:

sed -nr 's:(net.ipv[^=]*=)(.*):\2:p' /etc/sysctl.conf


All times are GMT -5. The time now is 08:18 AM.