Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. myFunction (line0
  2.  
  3. line1
  4.  
  5. line2
  6.  
  7. line3
  8.  
  9. line4)
  10.  
  11. myFunction ( )
  12.  
  13. $ awk '$0~/[(]/,$0~/[)]/{gsub(/line/,"newline")};1' input.txt
  14. another line
  15. something else
  16.  
  17. myFunction (newline0
  18.  
  19. newline1
  20.  
  21. whatever
  22.  
  23. newline2
  24.  
  25. newline3
  26.  
  27. newline4)
  28.  
  29. some other line
  30.  
  31. $ cat input.txt
  32. another line
  33. something else
  34.  
  35. myFunction (line0
  36.  
  37. line1
  38.  
  39. lilne2
  40.  
  41. line3
  42.  
  43. line4)
  44.  
  45. some other line
  46. $ ./edit_function_args.py input.txt
  47. another line
  48. something else
  49.  
  50. myFunction (newline0
  51.  
  52. newline1
  53.  
  54. newline2
  55.  
  56. newline3
  57.  
  58. line4)
  59.  
  60. some other line
  61.  
  62. #!/bin/bash
  63. # Use shell builtins, read, true, false, printf
  64. flag=false
  65. while IFS= read -r line
  66. do
  67. case "$line" in
  68. (*"("*) flag=true ;;
  69. esac
  70.  
  71. if $flag
  72. then
  73. line=${line//line/newline}
  74. fi
  75.  
  76. printf "%sn" "$line"
  77.  
  78. case "$line" in
  79. (*")"*) flag=false ;;
  80. esac
  81.  
  82. done < "$1"
  83.  
  84. $ perl -0777 -pe 's/([^)]+)/$&=~s|line|newline|gr/ge' ip.txt
  85. myFunction (newline0
  86.  
  87. newline1
  88.  
  89. newline2
  90.  
  91. newline3
  92.  
  93. newline4)
  94.  
  95. sed '/(/,/)/s/line/newline/g'
  96.  
  97. sed -e '/(/{' -e ':loop;s/(.*)/()/;t;N;b loop' -e '}'
  98.  
  99. sed -e '/(/{
  100. :loop
  101. s/(.*)/()/
  102. t
  103. N
  104. b loop
  105. }'
  106.  
  107. awk '
  108. function mysub(str) {
  109. if (str) gsub(/line/, "newline", str); return str
  110. }
  111. BEGIN {
  112. OFS=FS="("
  113. }
  114. NF>1 {
  115. if (FS=="(") {
  116. print $1,mysub($2); OFS=FS=")"
  117. } else {
  118. print mysub($1),$2; OFS=FS="("
  119. }
  120. next
  121. }
  122. {
  123. print FS=="(" ? $0 : mysub($0)
  124. }' /path/to/input
  125.  
  126. awk 'function m(s){gsub(/line/,"newline",s);return s}BEGIN{OFS=FS="("}NF>1{if(FS=="("){print $1,m($2);OFS=FS=")"}else{print m($1),$2;OFS=FS="("}next}{print FS=="("?$0:m($0)}' /path/to/input
  127.  
  128. line96
  129. line97 myFunction (line0
  130.  
  131. line1
  132.  
  133. line2
  134.  
  135. line3
  136.  
  137. line4) line98
  138. line99
  139.  
  140. line96
  141. line97 myFunction (newline0
  142.  
  143. newline1
  144.  
  145. newline2
  146.  
  147. newline3
  148.  
  149. newline4) line98
  150. line99
  151.  
  152. awk '
  153. BEGIN {
  154. OFS=FS="("
  155. }
  156. NF>1 {
  157. if (FS=="(") {
  158. print $1,""; OFS=FS=")"
  159. } else {
  160. print "",$2; OFS=FS="("
  161. }
  162. next
  163. }
  164. FS=="("' /path/to/input
  165.  
  166. line96
  167. line97 myFunction (
  168. ) line98
  169. line99
  170.  
  171. $ cat d.txt
  172. myOtherFunction (x as boolean
  173. y as integer
  174. d as string
  175. e as whatever)
  176.  
  177. myFunction (xx as boolean
  178. yy as integer
  179. dd as string
  180. ee as whatever)
  181.  
  182. $ cat d2.txt
  183. BrandNewFunction (xxx as integer
  184. yyy as boolean
  185. ddd as integer
  186. eee as whatever)
  187.  
  188. $ a="$(sed -n '/myFunction (/,/)/p' d.txt)" #isolates myFunction from the source file d.txt
  189. $ b="$(cat d2.txt)" #get contents of file d2.txt (BrandNewFunction)
  190. $ c="$(cat d.txt)" #get the whole source file d.txt
  191. $ echo "${c/$a/$b}" #in source file d.txt ($c) replace $a with $b (d2.txt)
  192. #Output:
  193. myOtherFunction (x as boolean
  194. y as integer
  195. d as string
  196. e as whatever)
  197.  
  198. BrandNewFunction (xxx as integer
  199. yyy as boolean
  200. ddd as integer
  201. eee as whatever)
  202.  
  203. $ a="$(sed -n '/myFunction (/,/)/p' d.txt)";b="$(cat d2.txt)";c="$(cat d.txt)";echo "${c/$a/$b}"
  204.  
  205. $ b="$(sed -n '/BrandNewFunction (/,/)/p' d2.txt)"
  206.  
  207. $ b="myFunction ( )"
  208.  
  209. sgrep -o '%r ' '(start .. end) extracting ("("__")")' < input_file
  210.  
  211. myFunction (line0
  212.  
  213. line1
  214.  
  215. (line2)
  216.  
  217. line3
  218.  
  219. line4)
  220.  
  221. anotherFun (x y)
  222.  
  223. myFunction ( )
  224.  
  225. anotherFun ( )
  226.  
  227. sed -z 's/([^)]*)/( )/g' inputfile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement