Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ibm midrange - how to use handle line feed in sed command

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 ( ). Everything works fine except the line feed character. Any idea how I might be able to get the line feed ( ) 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 Line 2", expecting that the will get translated into the carriage return and line-feed characters
QSH CMD('sed -e "s?&1? Line 1 
 Line 2 ?g" test.txt > test.out')

  1. The string replacement works, except the line feed ( ) charater. The result file, test.out, looks like below. The 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 \
 Line 2 ?g" test.txt > test.out')
QSH CMD('sed -e "s?&1? Line 1 \\n Line 2 ?g" test.txt > test.out')
question from:https://stackoverflow.com/questions/65924677/how-to-use-handle-line-feed-in-sed-command

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 
 Line 2", $0); print}'' test.txt > test.out')                                              

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...