sed not working properly with slash /


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed not working properly with slash /
# 1  
Old 09-25-2018
sed not working properly with slash /

my testfile is

Code:
aspsun1:usasp000$cat testfile
open connection
put $TMPDIR/SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133 <FILENAME>
quit

my problem statement is to replace the line with put command with 2 different lines a cd command and then put line.
from
HTML Code:
put $TMPDIR/SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133 <FILENAME>
to
HTML Code:
cd $TMPDIR
put SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133 <FILENAME>

for this i have used 4 variables
Code:
$echo $line
put $TMPDIR/SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133 <FILENAME>
$TO_DIR=`echo $line |awk -f" " '{print $2}' |awk -F"/" '{$NF=""; print $0}'|sed 's/ /\//g' `
FILE_NAME=`echo $line |awk -f" " '{print $2}' |awk -F"/" '{print $NF}' `
REM_PART=`echo $line |awk -f" " '{$1="";print $0}'|awk -f" " '{$1="";print $0}'`
$echo $TO_DIR
$TMPDIR/
$echo $FILE_NAME
SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133
$echo $REM_PART
<FILENAME>

but whenever i execute my command it just do not work, may be because of the '/' sign.
Code:
$sed 's/$line/cd $TO_DIR put $FILE_NAME $REM_PART/' testfile
open connection
put $TMPDIR/SUNIA.PJ080202.ENGRPTBZ.<OPERATOR>.133 <FILENAME>
quit

Please help !!
# 2  
Old 09-25-2018
without going into details of your script which has a number of issues.....
Code:
sed "s#$line#cd $TO_DIR put $FILE_NAME $REM_PART#" testfile


Last edited by vgersh99; 09-25-2018 at 01:54 PM..
# 3  
Old 09-25-2018
You can do this with bash builtins:
Code:
# read a line into words[0...n]
while read -a words
do
  if [ "${words[0]}" = "put" ]
  then
    wrd=${words[1]}
    # split $wrd on the rightmost "/"
    dir=${wrd%/*}
    file=${wrd##*/}
    # if there were no "/" then $dir==$wrd and $wrd==$file
    if [ "$dir" != "$wrd" ]
    then
      echo "cd $dir"
      words[1]=$file
    fi
  fi
  echo "${words[@]}"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed command not working properly

This is my sample file cat bipin.txt Unix is an OS Unix has its own commmands Unix is a user friendly OS Unix is platform independent Unix is a time sharing OS the best OS to learn is Unix Abinitio uses Unix in backend When i use sed 's/Unix/Linux/' bipin.txt , only the first... (2 Replies)
Discussion started by: Bipin_1991
2 Replies

2. Shell Programming and Scripting

Sed script not working properly on Solaris (works fine on AIX)?

Hi, I have a problem with a SED script that works fine on AIX but does not work properly on a Solaris system. The ksh script executes the SED and puts the output in HTML in tables. But the layout of the output in HTML is not shown correctly(no tables, no color). Can anyone tell if there is... (7 Replies)
Discussion started by: Faith111
7 Replies

3. Shell Programming and Scripting

mailx not working properly

I am using mailx command in my script to attach a file and send an email. I need to attach a csv file and send email to a mail id - I am using uuencode output.csv output.csv | mailx -s "test mail" xyz@abc.com This will send a mail with scrambled text in body. am i missing something ?... (4 Replies)
Discussion started by: Sriranga
4 Replies

4. Shell Programming and Scripting

Using sed to append backward slash before forward slash

Hi all, I need to know way of inserting backward slash before forward slash. My problem is that i need to supply directory path as an argument while invoking cshell script. This argument is further used in script (i.e. sed is used to insert this path in some file). So i need to place \ in front... (2 Replies)
Discussion started by: sarbjit
2 Replies

5. UNIX for Advanced & Expert Users

Sendmail is not working properly

Hi All, Can any one help me to solve the issue. The Issue is, i have started the sendmail service on my RHEL 4 update 6 box, I am able to send the mail from my box to almost all of the Email Id's except few. Exampe, test mail. . Output is :the message is sent. now if I send the... (2 Replies)
Discussion started by: akhtar.bhat
2 Replies

6. Shell Programming and Scripting

\n not working properly

Hi all, I'm trying to generate a series of txt files starting from a plain csv file part of my code: #!/bin/ksh INSTALLDIR=/Users/ME/Installdir CSV=CSV.csv TMP=/tmp/$(basename $0).txt tr -s "\r" "\n" < /$INSTALLDIR/$CSV > $TMP function Makefiles { printf '%24s:%30s\n' "sometext"... (1 Reply)
Discussion started by: Jive Spector
1 Replies

7. HP-UX

Cron - Not working properly

Hi, I do have three scripts. Whcih inserts records into a table using sqlldr, creating some reprot files etc. The first script will call the second and then the second will call the third. When I run my first script from the shell prompt, all my operation are completed successfully. If I do... (23 Replies)
Discussion started by: risshanth
23 Replies

8. UNIX for Dummies Questions & Answers

Telnet is not working properly

telnet at my system is behaving stange. Some times I am able to telnet to other machines but sometimes it stop doing that. Then i have to reboot the machine and most of the time (not 100%) it works. SImilar is the case with SSH. Sometime it works , some time it don't. i am new to Unix and I do not... (1 Reply)
Discussion started by: deepak_pathania
1 Replies

9. Programming

y is this not working properly?

#include <stdio.h> #include <sys/types.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> struct stat s; main() { char c; if (fork()==0) { system("clear"); do { printf("myAI\\>§ "); scanf("%s",c); if(stat(c,&s)>-1) {... (3 Replies)
Discussion started by: C|[anti-trust]
3 Replies

10. UNIX for Dummies Questions & Answers

Keyboard not working properly...

Hello Again, Those that have noticed my earlier posts will know that I have succesfully installed Solaris 8 onto my pc. I haven't been able to get x-server working (i think it doesn't like my video card) though I've been able to log into root (with a bit of help from unix forums :o ) and have... (2 Replies)
Discussion started by: timresh
2 Replies
Login or Register to Ask a Question