Domanda

I have a flat file separated by | that I want to update from information already inside of the flat file. I want to fill the third field using information from the first and second. From the first field I want to ignore the last two numbers when using that data to compare against the data missing the third field. When matching against the second field I want it to be exact. I do not want to create a new flat file. I want to update the existing file. I researched a way to pull out the first two fields from the file but I do not know if that will even be helpful for the goal I am trying to achieve. To sum all of that up, I want to compare the first and second fields to other fields in the file to pull the third field that may be missing on some of the lines on the flat file.

awk -F'|' -v OFS='|' '{sub(/[0-9 ]+$/,"",$1)}1 {print $1 "\t" $2}' tstfile

first field|second field|third field

Original intput:

t1ttt01|/a1

t1ttt01|/b1

t1ttt01|/c1

t1ttt03|/a1|1

t1ttt03|/b1|1

t1ttt03|/c1|1

l1ttt03|/a1|3

l1ttt03|/b1|3

l1ttt03|/c1|3

What it should do:

t1ttt03|/a1|1 = t1ttt01|/a1

when comparing t1ttt|/a1| = t1ttt|/a1

Therefore

t1ttt01|/a1 becomes t1ttt01|/a1|/1

What I want the Output to look like:

t1ttt01|/a1|1

t1ttt01|/b1|1

t1ttt01|/c1|1

t1ttt03|/a1|1

t1ttt03|/b1|1

t1ttt03|/c1|1

l1ttt03|/a1|3

l1ttt03|/b1|3

l1ttt03|/c1|3
È stato utile?

Soluzione

One way with awk:

awk '

# set the input and output field separator to "|"

BEGIN{FS=OFS="|"}

# Do this action when number of fields on a line is 3 for first file only. The
# action is to strip the number portion from first field and store it as a key
# along with the second field. The value of this should be field 3

NR==FNR&&NF==3{sub(/[0-9]+$/,"",$1);a[$1$2]=$3;next} 

# For the second file if number of fields is 2, store the line in a variable
# called line. Validate if field 1 (without numbers) and 2 is present in
# our array. If so, print the line followed by "|" followed by value from array.

NF==2{line=$0;sub(/[0-9]+$/,"",$1);if($1$2 in a){print line OFS a[$1$2]};next}1
' file file

Test:

$ cat file
t1ttt01|/a1
t1ttt01|/b1
t1ttt01|/c1
t1ttt03|/a1|1
t1ttt03|/b1|1
t1ttt03|/c1|1
l1ttt03|/a1|3
l1ttt03|/b1|3
l1ttt03|/c1|3
$ awk 'BEGIN{FS=OFS="|"}NR==FNR&&NF==3{sub(/[0-9]+$/,"",$1);a[$1$2]=$3;next}NF==2{line=$0;sub(/[0-9]+$/,"",$1);if($1$2 in a){print line OFS a[$1$2]};next}1' file file
t1ttt01|/a1|1
t1ttt01|/b1|1
t1ttt01|/c1|1
t1ttt03|/a1|1
t1ttt03|/b1|1
t1ttt03|/c1|1
l1ttt03|/a1|3
l1ttt03|/b1|3
l1ttt03|/c1|3
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top