I have a list of value , and need to replace that in my file.
Eg:
File1
tcname:fail
tcname: Pass
tcname:skipped
File2:
01,02,03
Output:
File 1
01:fail
02: Pass
03:Skipped
...
Code: Tried the below 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
$ 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
1 Like
awk -F": ?" 'NR==1 {split($0, arr, ",");next} {print arr[FNR]":"$2}' File2 File1
01:fail
02:Pass
03:skipped
1 Like
Hello DevAakash,
Could you please try following.
awk 'FNR==NR{for(i=1;i<=NF;i++){a=$i};next} {print a[FNR],$2}' FS="," Input_file2 FS=":" OFS=":" Input_file1
Thanks,
R. Singh
1 Like
May be so
grep -no "[^: ]\+$" <file1
--- Post updated at 11:13 ---
Else one
grep -o ":.*$" <file1 | nl -n rz
RudiC
6
How about (untested)
<file2 tr , $'\n' | paste -d: - <(cut -d: -f2 file1)
2 Likes
sed -n ':1;h;s/,.*//;N;s/\n[^:]*//;p;g;s/^[^,]*,//; t1' file2 file1
replace.sh:
#!/bin/bash
ext=new$$
for arg
do
set 01 02 03
changed=
while IFS= read line
do
case $line in
(*tcname*)
echo "${line//tcname/$1}"
shift
changed=1
;;
(*)
echo "$line"
;;
esac
done <"$arg" >"$arg.$ext" &&
[ -n "$changed" ] &&
cp "$arg.$ext" "$arg"
rm "$arg.$ext"
done
replace.sh file1
1 Like
Hi @MadeInGermany
How about replacing
rm "$arg.$ext"
with this ?
cat "$arg.$ext"
rm $_