1

I'm trying to use the sed command to replace a string in an IFS text file with another string that contains a line feed character (\n). Everything works fine except the line feed character. Any idea how I might be able to get the line feed (\n) to work? Below is what I've tried:

  1. The IFS text file name is test.txt. Below is the content of this file:
&1                                                        
  1. I ran the following command in an RPGLE program to replace the &1 with the text of "Line 1 \n Line 2", expecting that the \n will get translated into the carriage return and line-feed characters
QSH CMD('sed -e "s?&1? Line 1 \n Line 2 ?g" test.txt > test.out')

  1. The string replacement works, except the line feed (\n) charater. The result file, test.out, looks like below. The \n is being translated to character n:
Line 1 n Line 2 
  1. I've also tried below and none of them works:
QSH CMD('sed -e "s?&1? Line 1 \\\n Line 2 ?g" test.txt > test.out')
QSH CMD('sed -e "s?&1? Line 1 \\\\n Line 2 ?g" test.txt > test.out')
HockChai Lim
  • 1,675
  • 2
  • 20
  • 30

2 Answers2

0

I was unable to get sed to work. I ended up using awk utility to achieve it. Below is the awk command:

QSH CMD('export QIBM_CCSID=1252; awk ''{gsub(/&1/, "Line 1 \r\n Line 2", $0); print}'' test.txt > test.out')                                              
HockChai Lim
  • 1,675
  • 2
  • 20
  • 30
0

worked for me from call qp2term:

> echo "&1" > input.txt                                    
  echo "&1" > input.txt                                    
> cat input.txt                                            
  cat input.txt                                            
  &1                                                       
> sed -r 's/&1/Line 1 \nLine 2\n/' <input.txt >output.txt  
  sed -r 's/&1/Line 1 \nLine 2\n/' <input.txt >output.txt  
> cat output.txt                                           
  cat output.txt                                           
  Line 1                                                   
  Line 2                                                   

but, I get same behavior as you from QSH.

QSH CMD('sed -C 819  ''s/&1/Line 1 \nLine 2\n/'' <input.txt >output2.txt')

QSH CMD('cat output2.txt') 

Line 1 nLine 2n                      
Press ENTER to end terminal session. 

my guess is it has to do the the CCSID. Even with the -C 819 switch, the output file is CCSID 37 - ebcdic.

RockBoro
  • 2,163
  • 2
  • 18
  • 34
  • I think qsh and qp2term run in two different shell environments. I'm guessing that one support \n in sed and the other does not. I need to run sed from a RPGLE program via QCMDEXC call. So, I'm not able to use qp2term. But I'm able to get it to work using awk. Such a bummer to waste a day for a \n. – HockChai Lim Jan 28 '21 at 20:38
  • @HockChaiLim - While you can't use [QP2TERM](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzalf/rzalfterm.htm), you *can* use [QP2SHELL](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzalf/rzalfshell.htm) (or QP2SHELL2, if you want to run in the same activation group). You would use these instead of QCMDEXC. (Well, you *could* use QCMDEXC to issue the call to QP2SHELL, but why add another layer?) – John Y Jan 29 '21 at 14:59
  • you can also force which ever shell you want through `QSH CMD('exec /QOpenSys/usr/bin/csh ...')` https://www.ibm.com/docs/en/i/7.4?topic=ssw_ibm_i_74/apis/qp2shell.htm it is under the second usage note. – Peter Jan 06 '22 at 22:19