LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   sed options (http://www.linuxquestions.org/questions/programming-9/sed-options-94879/)

niidea 09-20-2003 12:33 PM

sed options
 
My question is : is there an equivalent for sed to the -v option of gawk ?

I'd like to run the following :
$ sed 'N c\TEXT' FILE
in a shell script to replace the Nth line of FILE by TEXT, where N and TEXT are variables of the script.

Maybe there is another solution ?

Thanks.

druuna 09-20-2003 12:54 PM

Using the correct quotes after the sed command will do the trick:
Code:

#!/bin/bash
#
# infile must be given

INFILE="$1"

# Are input/output file provided:
[[ -z ${INFILE} ]] && echo "No input file given. Quiting" && exit 1

TEXT="This is line number :"

for THIS in 1 2 3
do
  sed "${THIS}s/.*/${TEXT}${THIS}/" ${INFILE}
  echo ""
done

$ cat inputfile

wertwrtwertwertwertwert
sdfgsfdgsdfgsdfgsdfgsdfg
cvxcvbncvbncbncbncvbncvbn
345634563546356354635636

$ ./sed-options.sh inputfile
This is line number :1
sdfgsfdgsdfgsdfgsdfgsdfg
cvxcvbncvbncbncbncvbncvbn
345634563546356354635636

wertwrtwertwertwertwert
This is line number :2
cvxcvbncvbncbncbncvbncvbn
345634563546356354635636

wertwrtwertwertwertwert
sdfgsfdgsdfgsdfgsdfgsdfg
This is line number :3
345634563546356354635636
$

niidea 09-20-2003 01:15 PM

Okay, thanks a lot.


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