78124

string replacement in unix

Question:

Need a solution on how to solve this using SED or AWK command in UNIX.

I have a source file whose values have to be populated as following. (Values must be fetched from a reference file and update the source file).

Source file:

aaaa Uid=xxxx pwd=nnnn bbbb uid=yyyy pwd=eeee cccc uid=zzzz pwd=kkkk

Reference file:

block, parameter, value aaaa, uid, 1a1a aaaa, pwd, 1b1b bbbb, uid, 2a2a bbbb, pwd, 2b2b cccc, uid, 3a3a cccc, pwd, 3b3b

Output File:

aaaa Uid=1a1a pwd=1b1b bbbb uid=2a2a pwd=2b2b cccc uid=3a3a pwd=3b3b

Requirement:

For aaaa, SED must search for Uid within line number 1 and 3 and replace the value which is after the "=". Ie, xxxx with 1a1a.

For bbbb,SED must search for Uid within line number 5 and 7 and replace the value which is after the "=". Ie, yyyy with 2a2a.

Similarly for other parameters.

Thanks.

Answer1:

sed is for simple substitutions on individual lines. For any other text manipulation you should be using awk:

$ cat tst.awk BEGIN { FS = "[,[:space:]=]+" } NR==FNR { if (NR>1) { map[$1,$2] = $3 } next } { if (NF==1) { key = $0 } else { $0 = $1 "=" map[key,tolower($1)] } print } $ awk -f tst.awk ref.txt src.txt aaaa Uid=1a1a pwd=1b1b bbbb uid=2a2a pwd=2b2b cccc uid=3a3a pwd=3b3b

Recommend

  • string replacement using awk command
  • How to get the second value in a dropdown box based on the selected one in the first dropdown box
  • ERROR: Additional information: External table is not in the expected format
  • Two paths for the same method
  • How does socketcan handle arbitration?
  • inserting duplicate records with SQL
  • Java catching exceptions and subclases
  • Custom font for MKAnnotationView Callout
  • Java thread for JDBC
  • How to create L lists of n non-zero random decimals where each list sums to 1.0?
  • Pass large string into api controller by $http angular service
  • css Star-rating html
  • Add delivery info to query in SAP Crystal Reports
  • Parsing an XML response to a query
  • Windows batch string manipulation in loop
  • Blocks within NSOperation
  • Modifying files nested in tar archive
  • Sed: Using a,c or i in a one-liner
  • how to reopen a class in gems
  • XSD sequence shows as abiguous!
  • How to calculate total across columns but one?
  • How can I have equal heights for inner elements of flexbox grid/boxes/cards without using jQuery?
  • Django - can't get highchart to display data
  • Ruby: Why does this way of using map throw an error?
  • firebase, how to update data at a key
  • Attemping to change attributes of $(this) inside success function
  • How to generate an asynchronous reset verilog always blocks with chisel
  • How to align an image side by side with a heading element?
  • Can't access Tomcat 7 Manager app when running from Eclipse
  • findObjectsInBackgroundWithBlock block signature not correct
  • How gzip file gets stored in HDFS
  • Responsive left sidebar open close
  • Prevent page break in text block with iText, XMLWorker
  • C++ friend class std::vector
  • SyntaxError: (irb):26: both block arg and actual block given
  • rspec simple example getting error on request variable in integration test
  • Unable to decode certificate at client new X509Certificate2()
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Symfony2: How to get request parameter
  • ORA-29908: missing primary invocation for ancillary operator