Help using combining variables with sed command (RHEL 7)


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help using combining variables with sed command (RHEL 7)
# 1  
Old 01-23-2020
Help using combining variables with sed command (RHEL 7)

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

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

When I run this I get

Code:
$ 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)

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

Here is what the test file looks like before
Code:
SELECT:  ROK_NUMBER EQ 2388090

Here is what it looks like after
Code:
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 :/

Last edited by Scrutinizer; 01-23-2020 at 01:16 PM.. Reason: Additional code tags
# 2  
Old 01-23-2020
Hi, I noticed you use {7} in the script and {8} on the command line.
Other than that I cannot reproduce your error..
# 3  
Old 01-23-2020
Looks like the well known / infamous DOS line terminator problem:


Code:
$ 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 fora for many, many more options
These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 01-23-2020
Ugh, Ok thank you. I made some updates:

My script (rok.sh)
Code:
#!/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)

Code:
SELECT: ROK_NUMBER EQ 2388085

When I run the command in terminal

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

When I run the script from terminal

Code:
$ 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 ---

Quote:
Originally Posted by RudiC
Looks like the well known / infamous DOS line terminator problem:

So - get rid of that non-*nix feature. Use a *nix text editor, or the dos2unix command, or search these fora for many, many more options
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 Smilie

Quote:
$ sh -x rok.sh 2388123
+ ROK_NO=$'2388123\r'
+ RPT=$'/tmp/test\r'
'' '$RPT '/^SELECT/ s/(.{22}).{7}/\12388123
: No such file or directory
$
--- 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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use variables in 'sed' append command?

HELLO!! I'm trying to pass a variable with in the 'sed' command (which would add some piece of code to file at a particular line). We can use sed '{line-number}a\ alfjaljf\ aslfjsfsjafl\ adlfjaf\' file.txt If file.txt is Now, I would like to add the parameter 'lmn' after... (1 Reply)
Discussion started by: mjavalkar
1 Replies

2. Red Hat

Does RHEL 5 provide a command to collect RHEL system log in single compress file?

Hi, I heard a command that can collect all RHEL 5 log in a single compress file before I forget. Does any body know...What the command is ? Thanks. (4 Replies)
Discussion started by: nnnnnnine
4 Replies

3. Shell Programming and Scripting

Combining multiple variables into new variable

Hello, I am a new joiner to the forum, and have what i hope is a simple question, however I can't seem to find the answer so maybe it is not available within bash scripting. I intend to use the below script to archive files from multiple directories at once by using a loop, and a variable (n)... (10 Replies)
Discussion started by: dring
10 Replies

4. Shell Programming and Scripting

Variables in SED command

Hi all, I want write a script to display 5rows at times from a input file. my out like: echo " display started" r1 r2 r3 r4 r5 ... Some action items... again i need next 5 lines. can you please advise. (2 Replies)
Discussion started by: koti_rama
2 Replies

5. Shell Programming and Scripting

sed command using variables in shell script

hi guys, The following command doesn't seem to work in my shell script: tag=$(sed -n '/${line}/ s/.*\.*/\1/p' myfile.txt) When i replace the ${line} with an actual value, it works fine. So, how do i use the ${line} in this sed command? Thanks in advance, Zaff (2 Replies)
Discussion started by: zaff
2 Replies

6. Shell Programming and Scripting

Problem combining two variables into one

Hello, I have a problem combining two variables into one. I did the following: in my env variables i had set PATH_DESTINATION_1=/root/path_one PATH_DESTINATION_2=/root/path_two #!/usr/bin/ksh count=1 count_path=2 while do (3 Replies)
Discussion started by: Eraser
3 Replies

7. Shell Programming and Scripting

Combining two variables in ksh

I can't believe I can't figure this out... given this code: CARS_DATA_LIST=`cat /tmp/file1 | awk '{print $1}' ` FMSA_DATA_LIST=`cat /tmp/file2 | awk '{print $1}' ` The value of each of the above variables is: CARS = a b c d e f g FMSA = a b c q r s I want to declare a third... (8 Replies)
Discussion started by: Shoeless_Mike
8 Replies

8. Shell Programming and Scripting

sed: command garbled - when executed with $variables

I have got a file job_tree.lst , which contains 1 line as below $ cat job_tree.lst ABC_D_EQ_XXXXX_YYYY_PRD_UX 05/08/2009 05:47 05/08/2009 05:50 IN 13412850/0 I want to get a child job string 3 characters offset from the parent job string (i.e. ABC_D_EQ_XXXXX_YYYY_PRD_UX 05/08/2009... (4 Replies)
Discussion started by: Armaan_S
4 Replies

9. UNIX for Dummies Questions & Answers

sed command not work with variables?

I am trying to write a simple script which will take a variable with sed to take a line out of a text and display it #!/bin/sh exec 3<list while read list<&3 do echo $list sed -n '$list p'<list2 done this does not work, yet when I replace the $list variable from the sed command and... (1 Reply)
Discussion started by: MaestroRage
1 Replies

10. Shell Programming and Scripting

assigning variables in sed command

I need to assign a variable within a variable in a sed command. I tried doing the following in c shell. set left = 1 set right = 2 set segment = qwerty sed -n -e "/$segment{$left}/,/$segment{$right}/p" file.txt what is wrong with this syntax? (3 Replies)
Discussion started by: wxornot
3 Replies
Login or Register to Ask a Question