Awk or Sed, fubd match in column, then edit column.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk or Sed, fubd match in column, then edit column.
# 1  
Old 02-04-2011
Awk or Sed, fubd match in column, then edit column.

FILE A:
Code:
9780743551526,(Abridged)
9780743551779,(Unabridged)
9780743582469,(Abridged)
9780743582483,(Unabridged)
9780743563468,(Abridged)
9780743563475,(Unabridged)

FILE B:
Code:
c3saCandyland    9780743518321    "CANDYLAND"    "MCBAIN, ED"    2001 
c3sbCandyland    9780743518321    "CANDYLAND"    "MCBAIN, ED"    2001 
c4saCandyland    9780743518321    "CANDYLAND"    "MCBAIN, ED"    2001 
c4sbCandyland    9780743518321    "CANDYLAND"    "MCBAIN, ED"    2001 
c1sA Fourth Mega Market    9780743518482    "FOURTH MEGA  MARKET"    "ACAMPORA, RALPH"    2004

I need, that if the 13 digit number from FILE A: matches the 13 digit number in the second column of FILE B, to then print the word (Abridged) or (Unabridged) with the parentheses, depending on whichever was next to the number in FILE A, to the end of the title, which is the first thing in quotes on each line in FILE B.

Example:
Code:
FILE A:
9780743551526,(Abridged)

FILE B:
c3sbCandyland    9780743551526    "CANDYLAND"    "MCBAIN, ED"    2001  

Desired Output:
c3sbCandyland    9780743551526    "CANDYLAND (Abridged)"    "MCBAIN, ED"    2001

# 2  
Old 02-04-2011
Code:
awk 'NR==FNR{split($0,s,",");a[s[1]]=s[2];next}$2 in a{sub("\"$"," "a[$2]"\"",$3)}1' a b

# 3  
Old 02-04-2011
Code:
gregg@ubuntu1:~$ cat filea
9780743551526,(Abridged)
gregg@ubuntu1:~$ cat fileb
c3sbCandyland 9780743551526 CANDYLAND MCBAIN, ED 2001
gregg@ubuntu1:~$ awk 'NR==FNR{split($0,s,",");a[s[1]]=s[2];next}$2 in a{sub("\"$"," "a[$2]"\"",$3)}1' filea fileb
c3sbCandyland 9780743551526 CANDYLAND MCBAIN, ED 2001
gregg@ubuntu1:~$

Doesnt seem to work, am I missing something?

Last edited by Scott; 02-04-2011 at 09:20 PM.. Reason: Code tags, PLEASE...
# 4  
Old 02-04-2011
try:
Code:
awk 'NR==FNR{sub(","," ");a[$1]=$2}NR>FNR{for(i=1;i<NF;i++) {if($i~/^[0-9]../){$(i+1)="\""$(i+1)" "a[$i]"\""}printf $i"\t"}print ""}'  file1 FS="\t"  file2


Last edited by yinyuemi; 02-04-2011 at 10:07 PM..
# 5  
Old 02-06-2011
Code:
awk -F, '
NR==FNR{a[$1]=$2;next} 
{for (i in a) {if ($0~i) $2=$2 " " a[i]}}1
' FileA FS="\"" OFS="\"" FileB

This User Gave Thanks to rdcwayx For This Post:
# 6  
Old 02-07-2011
Yinuemi, your code is fast, but is not completely correct. it strips the date at the end and adds many extra quotes. Rdcwayx, It appears you have fround the solution! I am checking it now. your code works much slower than Yinuemi, but gives the correct output. Thank you!

---------- Post updated at 09:47 AM ---------- Previous update was at 09:43 AM ----------

RDC, I would love it if you could explain your code a bit.. I know basic AWK but this is beyond my level.
# 7  
Old 02-07-2011
Hi Glev, you can try this:
Code:
awk 'NR==FNR{sub(","," ");a[$1]=$2}
NR>FNR{for(i=1;i<=NF;i++) {
if($i~/^[0-9]...../){$(i+1)=substr($(i+1),1,length($(i+1))-1)" "a[$i]"\""}printf $i"\t"}print ""}' fileA  fileB

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed summation of one column based on some entry in first column

Hi All , I am having an input file as stated below Input file 6 ddk/djhdj/djhdj/Q 10 0.5 dhd/jdjd.djd.nd/QB 01 0.5 hdhd/jd/jd/jdj/Q 10 0.5 512 hd/hdh/gdh/Q 01 0.5 jdjd/jd/ud/j/QB 10 0.5 HD/jsj/djd/Q 01 0.5 71 hdh/jjd/dj/jd/Q 10 0.5 ... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

3. UNIX for Dummies Questions & Answers

Match sum of values in each column with the corresponding column value present in trailer record

Hi All, I have a requirement where I need to find sum of values from column D through O present in a CSV file and check whether the sum of each Individual column matches with the value present for that corresponding column present in the trailer record. For example, let's assume for column D... (9 Replies)
Discussion started by: tpk
9 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
3 Replies

6. Shell Programming and Scripting

awk/sed to extract column bases on partial match

Hi I have a log file which has outputs like the one below conn=24,196 op=1 RESULT err=0 tag=0 nentries=9 etime=3,712 dbtime=0 mem=486,183,328/2,147,483,648 Now most of the time I am only interested in the time ( the first column) and a column that begins with etime i.e... (8 Replies)
Discussion started by: pkabali
8 Replies

7. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

8. Shell Programming and Scripting

awk match second column

Hi I have file which looking like: fox_spectrum fox_spectrum\ fox_spectrum (fox_spectrum)\ I just want lines which did not begin with brackets () in second column I tried: awk -v var='^(.*' '{if ($2!=var) print $0}' file but it returns whole file, thanks a lot (8 Replies)
Discussion started by: wakatana
8 Replies

9. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

10. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies
Login or Register to Ask a Question