0

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.

Jotne
  • 40,548
  • 12
  • 51
  • 55
Ra-V
  • 69
  • 2
  • 10
  • use the edit button and format the question in a readable way, please. Also, this looks quite similar to your previous question. Did you use its answers? – fedorqui Mar 10 '15 at 12:00
  • It doesnt work the way I intended. Specifying the line numbers in SED is a way of doing it. But in my case I am passing all the values are variables, so facing some issues. BTW, this was the best suited suggestion given...... sed -e '$str_val,${end_val}s/string/replace/' filename.txt...... This worked for only 1 occurrance, say for "aaaa". – Ra-V Mar 10 '15 at 12:11
  • As I said before: use the `edit` button to add this information into the question and make the question more clear. – fedorqui Mar 10 '15 at 12:12
  • sorry...was unaware of that. – Ra-V Mar 10 '15 at 13:11

1 Answers1

2

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
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    I will check and surely will let you know... Thanks!! :) – Ra-V Mar 10 '15 at 14:18
  • With mawk 1.3.3 (given bu `awk -W version`) I had to replace `[:space:]` on the first line by a literal space (and should have added tab,..) – Walter A Mar 10 '15 at 16:08
  • then get rid of mawk and get a POSIX awk at least since who knows what else might be broken :-). Seriously - get GNU awk, you'll thank yourself later. – Ed Morton Mar 10 '15 at 18:08
  • @EdMorton - Thanks... :-) .. It works perfectly... Now I have a couple of doubts here... Firstly, How to fetch the values from the reference(lookup file), if it has two columns(value1 and value2) .. ? I either need values from "value1" column or "value2" column...based on the input parameter I supply to the script... Secondly, if the source file has got other records for which there is no lookup data in the reference file... the script must print its original value.... how to do this??? Sorry,, if my doubts are so amature, as I am very new to shell scripting.... :-) – Ra-V Mar 12 '15 at 05:22
  • @EdMorton - Also.. What if the lookup data in the reference file are scrambled...like if cccc comes before bbbb set??? I need a solution for this too... It would be great if you can provide! :-) – Ra-V Mar 12 '15 at 09:50
  • For your first comment above, post a new question with sample input and expected output to show what you are trying to describe. For your second comment, the input order has no effect on the resultant `map[]` - try it. – Ed Morton Mar 12 '15 at 12:14
  • @EdMorton - Posted my query in this same page(in the answers section)... Please refer and let me know if you need further details! :-) – Ra-V Mar 12 '15 at 13:40
  • As I said, post a new question and format the input/output correctly and make it testable - i.e. make sure the output matches the input and get rid of the `...`s. And again - the answer I have provided is independent of any order in the input files - TRY IT rather than assuming there will be an issue if the order changes. – Ed Morton Mar 12 '15 at 13:46
  • @EdMorton -- Apologies! :-) .... Please follow the link for the updated query.... [link]http://stackoverflow.com/questions/29012648/string-replacement-using-awk-command – Ra-V Mar 12 '15 at 14:38