Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Replace matches string in each line with the arrayvalue in shell


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Replace matches string in each line with the arrayvalue in shell

I have a list of value , and need to replace that in my file.
Eg:
Code:
File1
tcname:fail
tcname: Pass
tcname:skipped

File2:
Code:
01,02,03

Output:
Code:
File 1
01:fail
02: Pass
03:Skipped
...

Code: Tried the below code
Code:
id="01,02,03"
arr=$(echo $id | tr "," "\n")
for i in "${arr[@]}"; do
         replace="$i"
         sed -i "s/tcname/$replace/g" replace.txt       
          done

Moderator's Comments:
Mod Comment Please do wrap your samples/codes in CODE TAGS as per forum rules.

Last edited by RavinderSingh13; 05-22-2019 at 04:11 AM..
# 2  
Code:
$ awk -F: -v list="01,02,03" ' NR==1 { split(list,arr,",") } { sub("tcname",arr[NR]) } 1 ' OFS=: File1
01:fail
02: Pass
03:skipped

This User Gave Thanks to anbu23 For This Post:
# 3  
Code:
awk -F": ?" 'NR==1 {split($0, arr, ",");next} {print arr[FNR]":"$2}' File2 File1
01:fail
02:Pass
03:skipped

This User Gave Thanks to nezabudka For This Post:
# 4  
Hello DevAakash,

Could you please try following.
Code:
awk 'FNR==NR{for(i=1;i<=NF;i++){a[i]=$i};next} {print a[FNR],$2}' FS=","  Input_file2  FS=":" OFS=":"  Input_file1

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
May be so Smilie
Code:
grep -no "[^: ]\+$" <file1

--- Post updated at 11:13 ---

Else one
Code:
grep -o ":.*$" <file1 | nl -n rz

# 6  
How about (untested)

Code:
<file2 tr , $'\n' | paste -d: - <(cut -d: -f2 file1)

These 2 Users Gave Thanks to RudiC For This Post:
# 7  
Code:
sed -n ':1;h;s/,.*//;N;s/\n[^:]*//;p;g;s/^[^,]*,//; t1' file2 file1

Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Replace string of a file with a string of another file for matches using grep,sed,awk
Paras Pandey
I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->...... Shell Programming and Scripting
1
Shell Programming and Scripting
Replace all string matches in file with unique random number
funkman
Hello Take this file… Test01 Ref test Version 01 Test02 Ref test Version 02 Test66 Ref test Version 66 Test99 Ref test Version 99 I want to substitute every occurrence of Test{2} with a unique random number, so for example, if I was using sed, substitution would be something...... Shell Programming and Scripting
1
Shell Programming and Scripting
Get line number when matches a string
Muthuraj K
If I have a file something like as shown below, ARM*187878*hjhj BAG*88778*jjjj COD*7777*kkkk BAG*87878*kjjhjk DEF*65656*89989*khjkk I need the line numbers to be added with a colon when it matches the string "BAG". Here in my case, I need something like ARM*187878*hjhj...... Shell Programming and Scripting
4
Shell Programming and Scripting
Need to find a string, check the next line, and if it matches certain criteria, replace it with a s
midniteslice
Hey Fellas. I am new to scripting. I have searched through the forums and found a lot of good info, but I can't seem to get any of it to work together. I am trying to find a particular sting in a file, and if the next string matches certain criteria, replace it with a string from a csv...... Shell Programming and Scripting
6
Shell Programming and Scripting
Replace Entire line if any part matches regexp
Cocoabean
Hey guys, I have a file that I've slowly been awking, seding, and greping for data entry. I am down to pull the addresses out to insert them into an excel file. Each address is a few lines, but i want to put a semicolon delimiter in between each address so I can export the text file into excel and...... Shell Programming and Scripting
6
Shell Programming and Scripting

Featured Tech Videos