Help using combining variables with sed command (RHEL 7)

Here is the whole script, very simple, but I am just learning

ROK_NO=$1
RPT=/tmp/test
sed -E '/^SELECT/ s/(.{23}).{8}/\1'"$ROK_NO"' /' $RPT
echo $RPT

When I run this I get

$ bash rok.sh 2388085
: No such file or directory
/tmp/test

When I type the command in console, it works (so I know there is a file there)

sed -E '/^SELECT/ s/(.{23}).{7}/\12388099 /' /tmp/test

Here is what the test file looks like before

SELECT:  ROK_NUMBER EQ 2388090

Here is what it looks like after

SELECT:  ROK_NUMBER EQ 2388099

That great but I want to use a script so I pass that number as an argument and use the $RPT variable? I'm sure it's something to do with the characters in the sed command and how I'm incorrectly using " vs ' but I cannot figure it out after hours of fiddling :confused:

Hi, I noticed you use {7} in the script and {8} on the command line.
Other than that I cannot reproduce your error..

Looks like the well known / infamous DOS line terminator problem:

$ RPT=test
$ sed 'p' $RPT
sed: can't read test: No such file or directory
$ RPT=test$'\r'
$ sed 'p' $RPT
 : No such file or directory

The first half of the error message is overwritten when the <carriage return> at the file name's end is performed.

So - get rid of that non-*nix feature. Use a *nix text editor, or the dos2unix command, or search these forums for many, many more options

2 Likes

Ugh, Ok thank you. I made some updates:

My script (rok.sh)

#!/bin/bash
ROK_NO=$1
RPT=/tmp/test
sed -E '/^SELECT/ s/(.{22}).{7}/\1'"$ROK_NO"'/' '$RPT'
#sed -E '/^SELECT/ s/(.{22}).{7}/\12388055/' '/tmp/test'

My file (/tmp/test)

SELECT: ROK_NUMBER EQ 2388085

When I run the command in terminal

$ sed -E '/^SELECT/ s/(.{22}).{7}/\12388123/' '/tmp/test'
SELECT: ROK_NUMBER EQ 2388123
$

When I run the script from terminal

$ bash rok.sh 2388123
: No such file or directory
$

Thanks for taking a look. I'm not sure what else I can do explain.

--- Post updated at 10:14 PM ---

Ok I will try that. I am using Textpad on my Windows PC then FTP the script file over to a Linux server where I'm trying to execute the script. I was just looking at this and was wondering what it all meant :o

--- Post updated at 10:22 PM ---

Dang man, that definitely did the trick. I didn't think I could mess up 2 variables and it was really making me sad. You saved the day -- thank you!