Replace matches string in each line with the arrayvalue in shell


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Replace matches string in each line with the arrayvalue in shell
# 1  
Old 05-22-2019
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  
Old 05-22-2019
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  
Old 05-22-2019
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  
Old 05-22-2019
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  
Old 05-22-2019
May be so Smilie
Code:
grep -no "[^: ]\+$" <file1

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

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

# 6  
Old 05-22-2019
How about (untested)

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

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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string works on command-line but fails when run from shell script

I wish to replace "\\n" with a single white space. The below does the job on command-line: $ echo '/fin/app/scripts\\n/fin/app/01/sql' | sed -e 's#\\\\n# #g'; /fin/app/scripts /fin/app/01/sql However, when i have the same code to a shell script it is not able to get me the same output:... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

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 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

3. Shell Programming and Scripting

Replace all string matches in file with unique random number

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... (1 Reply)
Discussion started by: funkman
1 Replies

4. Shell Programming and Scripting

Print entire line only if certain fixed character matches the string

Hi All, I have a file testarun.txt contains the below lines and i want to print the lines if the character positions 7-8 matches 01. 201401011111 201401022222 201402013333 201402024444 201403015555 201403026666 201404017777 201404028888 201405019999 201405020000 I am trying the... (4 Replies)
Discussion started by: Arunprasad
4 Replies

5. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

6. Shell Programming and Scripting

Get line number when matches a string

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... (4 Replies)
Discussion started by: Muthuraj K
4 Replies

7. Shell Programming and Scripting

Need to find a string, check the next line, and if it matches certain criteria, replace it with a s

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... (6 Replies)
Discussion started by: midniteslice
6 Replies

8. Shell Programming and Scripting

Does Sed Search/Replace Work For Multiple Matches On The Same Line?

Hello, I would like to delete all the footnotes in all my htm files. Hence, I have to delete the whole font tag pairs, i.e. deleting everything between the begin/end font tags. I create a testfile, of which data parts of all four lines are the same except for the number of font tag pairs,... (3 Replies)
Discussion started by: cibalo
3 Replies

9. Shell Programming and Scripting

Replace Entire line if any part matches regexp

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... (6 Replies)
Discussion started by: Cocoabean
6 Replies
Login or Register to Ask a Question