sed not working properly with slash /


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed not working properly with slash /
# 1  
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  
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 02:54 PM..
# 3  
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 for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #420
Difficulty: Medium
JavaScript supports much of the structured programming syntax from C (e.g., if statements, while loops, switch statements, do while loops, etc.).
True or False?

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

Why is sort not working properly here ?

Platform: RHEL 5.4 In the below text file I have strings like following. $ cat /tmp/mytextfile.txt DISK1 DISK10 DISK101 DISK102 DISK103 DISK104 DISK105 DISK106 DISK107 DISK108 DISK109 DISK110 DISK111 DISK112 DISK113 DISK114 (8 Replies)
Discussion started by: kraljic
8 Replies

3. UNIX for Dummies Questions & Answers

~c is not working properly with -r option

Hi There, --------- file1 ------- ~c asd@ac.com -------------- Now i am using below command cat file1|mailx -s " testing" -r " My Name" abc@tech.com (3 Replies)
Discussion started by: Tapan Sharma
3 Replies

4. Linux

rexec not working properly

Hi, I am trying to enable rexec to automate certain tasks(it has to be rexec, not ssh or any other due to the system environment), so after switching to linux, I followed the certain instructions that were laid out in the web. My operating system is fedora 17, so I first installed the... (1 Reply)
Discussion started by: wringer
1 Replies

5. 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

6. Red Hat

sudo is not working properly

This is the first time for using sudo for me. # visudo ## Allows people in group admin to run all commands %admin ALL=(ALL) ALL # groupadd admin # useradd temp # usermod -a -G admin temp # id temp uid=506(temp) gid=506(temp) groups=506(temp),507(admin) # #sudo... (5 Replies)
Discussion started by: getrue
5 Replies

7. 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

8. 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

9. 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

10. 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