সেড - ফাইলের সামগ্রীগুলির সাথে স্ট্রিং প্রতিস্থাপন করুন


22

আমার দুটি ফাইল রয়েছে: file1এবং file2।

file1 নিম্নলিখিত বিষয়বস্তু রয়েছে:

---
  host: "localhost"
  port: 3000
  reporter_type: "zookeeper"
  zk_hosts: 
    - "localhost:2181"

file2একটি আইপি ঠিকানা রয়েছে ( 1.1.1.1)

আমি যা করতে চাই তা প্রতিস্থাপন করা localhostহয় 1.1.1.1, যাতে শেষ ফলাফলটি হয়:

---
  host: "1.1.1.1"
  port: 3000
  reporter_type: "zookeeper"
  zk_hosts: 
    - "1.1.1.1:2181"

আমি চেষ্টা করেছি:

sed -i -e "/localhost/r file2" -e "/localhost/d" file1
sed '/localhost/r file2' file1 |sed '/localhost/d'
sed -e '/localhost/r file2' -e "s///" file1

তবে আমি হয় পুরো লাইনটি প্রতিস্থাপন করেছি, বা আইপি আমার লাইফের পরিবর্তনের পরে লাইনে যাবে।


1
নিশ্চিত না, কিন্তু cat file1 | sed -e 's/localhost/1.1.1.1/g'কাজ করে?
— dchirikov

1
এ \rsed কমান্ড।
— কেভিন

উত্তর:


14

এখানে একটি sedসমাধান:

% sed -e "s/localhost/$(sed 's:/:\\/:g' file2)/" file1
---
  host: "1.1.1.1"
  port: 3000
  reporter_type: "zookeeper"
  zk_hosts: 
    - "1.1.1.1:2181"

sed -iপরিবর্তনটি স্থানান্তর করতে আপনার ব্যবহার করা উচিত ।

আপনি যদি ব্যবহার করতে পারেন তবে awkএখানে করার এক উপায়:

% awk 'BEGIN{getline l < "file2"}/localhost/{gsub("localhost",l)}1' file1
---
  host: "1.1.1.1"
  port: 3000
  reporter_type: "zookeeper"
  zk_hosts: 
    - "1.1.1.1:2181"

4
জন্য +1 awk। আমি ধারণা করি sed এটি সক্ষম, তবে এটি ভয়াবহভাবে আনাড়ি হবে। এখানেই awkজ্বলে!
— হ্যালোসঘস্ট

1
@ হ্যালসগোস্ট: এটি আমি এবং আপনি উভয়ই ওপি প্রশ্নকে ভুল বুঝেছি বলে মনে হয়েছে, আমি আমার উত্তর আপডেট করেছি।
— cuonglm

sedফাইলটিতে শূন্যস্থান বা গ্লোব অক্ষর রয়েছে এমন ক্ষেত্রে সমাধানের জন্য কমান্ড প্রতিস্থাপনের দ্বিগুণ উদ্ধৃতি দেওয়া উচিত।
— গ্রিম

@ গ্র্যাম: ধন্যবাদ, আপডেট হয়েছে! একটি সম্পাদনা করতে নির্দ্বিধায়।
— cuonglm

2
আপনার বিকল্প /এবং উভয়ই পালাতে হবে &। তা"$(sed 's:[/\\&]:\\&:g' file2)"
— টবির স্পিড

6

শেল কমান্ডের বিকল্প ব্যবহারের আগে আপনি রিপ্লেসমেন্ট স্ট্রিং দিয়ে ফাইলটি পড়তে পারেন sed। সুতরাং sedএকটি সাধারণ বিকল্প দেখতে পাবেন:

sed "s/localhost/$(cat file2)/" file1 > changed.txt


18
এটি কি বিশেষ অক্ষরের সাথে বহু-রেখাযুক্ত ফাইল এবং ফাইলগুলিতে কাজ করে?
— ট্রেভর হিকি

9
পছন্দ করুন শেড কেবল একটি "নির্বিঘ্নিত` s "কমান্ড" ত্রুটি দিয়ে ব্যর্থ হয়।
— দারিওপি

1

ব্যবহার করার চেষ্টা করুন

join file1 file2

এবং তারপরে, কোনও অযাচিত ক্ষেত্র সরিয়ে ফেলুন।


1

আমারও আজ এই "সমস্যা" ছিল: অন্য ফাইলের সামগ্রীর সাথে পাঠ্যের একটি ব্লক কীভাবে প্রতিস্থাপন করা যায় to

আমি এটি একটি ব্যাশ ফাংশন তৈরি করে সমাধান করেছি (এটি স্ক্রিপ্টগুলিতে পুনরায় ব্যবহার করা যেতে পারে)।

[cent@pcmk-1 tmp]$ cat the_function.sh
# This function reads text from stdin, and substitutes a *BLOCK* with the contents from a FILE, and outputs to stdout
# The BLOCK is indicated with BLOCK_StartRegexp and BLOCK_EndRegexp
#
# Usage:
#    seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
function substitute_BLOCK_with_FILEcontents {
  local BLOCK_StartRegexp="${1}"
  local BLOCK_EndRegexp="${2}"
  local FILE="${3}"
  sed -e "/${BLOCK_EndRegexp}/a ___tmpMark___" -e "/${BLOCK_StartRegexp}/,/${BLOCK_EndRegexp}/d" | sed -e "/___tmpMark___/r ${FILE}" -e '/___tmpMark___/d'
}

[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/FileWithContents
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ source the_function.sh
[cent@pcmk-1 tmp]$ seq 100 110 | substitute_BLOCK_with_FILEcontents '^102' '^104' /tmp/FileWithContents > /tmp/result.txt
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$
[cent@pcmk-1 tmp]$ cat /tmp/result.txt
100
101
We have deleted everyhing between lines 102 and 104 and
replaced with this text, which was read from a file
105
106
107
108
109
110
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.