Find the answer to your Linux question:
Results 1 to 2 of 2
hi there, I'm having issue when trying to change a line in a file Code: ip addr show "$1" | awk "/^.*inet.*$1$/{print $2}" | sed -n '1 s,/.*,,p' to Code: ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    1

    Cool sed special caracters [bash script] issue

    hi there,

    I'm having issue when trying to change a line in a file

    Code:
    ip addr show "$1" | awk "/^.*inet.*$1$/{print $2}" | sed -n '1 s,/.*,,p'
    to
    Code:
    ip -4 -o addr show primary dev $1 | awk '$3 == "inet" {print $4; exit}' | sed 's#/.*##'
    this is what I've done
    Code:
     sed -i "s/ip addr show \"$1\" | awk \"/^.*inet.*$1\$/{print \$2}\" | sed -n \'1 s,/.*,,p\'\/ip -4 -o addr show primary dev $1 | awk \'$3 \=\= "inet" {print $4; exit}\' | sed \'s#\/.*##\'/g" /etc/xen/scripts/vif-common.sh
    sed- i "s/find/change/g" /etc/xen/scripts/vif-common.sh

    can someone help me??

    thanks

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    You seem to be wanting to replace a line in a shell script.

    I find that situations like this cause me to make mistakes in quoting. The shell tries to interpret too much of the replacement line, so I use an additional file, "hiding" as much as possible so that the shell will not "see" the line. That avoids the task of trying out-think the shell for quoting.

    With the replacement line in the additional file, you skip to the place where you want to replace the line, read the new line in, and print it. That effectively discards the old line. For any other line, you simply print the line, like this:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate how to avoid complex quoting.
    
    # Utility functions: print-as-echo, print-line-with-visual-space.
    pe() { for i;do printf "%s" "$i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    
    pl " Input file data1:"
    cat data1
    
    pl " Replacement file data2:"
    cat data2
    
    pl " Results from awk:"
    awk '
    /ip addr show/	{ getline replacement < "data2" ; print replacement ; next }
    	{ print }
    ' data1
    
    exit 0
    producing:
    Code:
    % ./s1
    
    -----
     Input file data1:
    one
    two
    ip addr show "$1" | awk "/^.*inet.*$1$/{print $2}" | sed -n '1 s,/.*,,p'
    four
    five
    
    -----
     Replacement file data2:
    ip -4 -o addr show primary dev $1 | awk '$3 == "inet" {print $4; exit}' | sed 's#/.*##'
    
    -----
     Results from awk:
    one
    two
    ip -4 -o addr show primary dev $1 | awk '$3 == "inet" {print $4; exit}' | sed 's#/.*##'
    four
    five
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...