2

I am working with this student's info:

name: Romeo
e_mail: letsrock@hotmail.com
Room: 0/48
street: 1/0/48

name: April
e_mail: thebestmonth@hotmail.com
Room: 0/4
street: 1/0/4

name: Julian
e_mail: julian688@gmail.com
Room: 0/2
street: 1/0/2

name: Charles
e_mail: charlesuniv@yahoo.com
Room: 0/1
street: 1/0/1

name: Chris
e_mail: todoras@hotmail.com
Room: 0/42
street: 1/0/42

name: Richard
e_mail: richiisfun@gmail.com
Room: 0/6
street: 1/0/6

I also have this .csv file:

id,name,e_mail
st0001, Romeo, letsrock@hotmail.com
st0002, Julian, julian688@gmail.com
st0003, Chris, todoras@hotmail.com
st0004, Richard, richiisfun@gmail.com

I would like to take the id from .csv file and add it to the .dat file, like this:

name: Romeo
e_mail: letsrock@hotmail.com
Room: 0/48
street: 1/0/48
id: st0001    

name: April
e_mail: thebestmonth@hotmail.com
Room: 0/4
street: 1/0/4

name: Julian
e_mail: julian688@gmail.com
Room: 0/2
street: 1/0/2
id: st0002

name: Charles
e_mail: charlesuniv@yahoo.com
Room: 0/1
street: 1/0/1

name: Chris
e_mail: todoras@hotmail.com
Room: 0/42
street: 1/0/42
id: st0003

name: Richard
e_mail: richiisfun@gmail.com
Room: 0/6
street: 1/0/6
id: st0004

So far I have tried this:

#!/bin/bash

    FILE1=students.dat
    FILE2=table.csv

    while read line; do
        if [[ $line == name* ]]; then
            echo -e "\n$line"
            expectIp=1
        elif [[ $line == *e_mail* && $expectIp -eq 1 ]]; then
            sed 's/^\s*//' <<< $line
            unset expectIp
        elif [[ $line == Room* ]]; then
            Room=$(echo $line | grep -o 'Room[^,]*,' | sed 's/,//')
            echo $Room
            echo $line | grep -o 'street*'
            justRoom=$(echo $Room | sed 's/Room: //')
            grep -A1 \"$justRoom\" $FILE2 | grep -o 'id'
        fi
    done < $FILE1

The last script has lots of errors.

I have found how to get the id from the .csv file:

grep  "richiisfun@gmail.com" students.csv | awk -F "\"*,\"*" '{print $1}'

How can I add it to the .dat file automatically?

2
perl -F',\s+' -lane '
   @ARGV and $h{$F[1]}=$F[0],next;

   /^name:\s+(\S+)/ && exists $h{$a=$1} .. /^$/ || eof and do{
      /^$/ || eof and $_ .= (/^$/ ? $, : $\) . ("id: " . $h{$a} // "") . (eof ? $, : $\);
   };

   print;
' table.csv students.dat

Explanation

Perl options

  • -F Field Separator is set to ,\s+
  • -l Output Record Separator is set to \n
  • -a Autosplit mode => array @F contains fields $1,$2,...,$NF
  • -n Print only when asked to + implicit readin of lines, a.la., awk

Logic

  • Order of arguments is .csv file then .dat file.
  • @ARGV and => when input file under consideration is .csv, otw .dat
  • From the .csv file data, populate the hash %h with keys as Name and value as ID.
  • Now when we come to process the .dat file is the where the action is
  • In Perl, condition1 .. condition2 is a flip-flop operator, a.la., , of sed. But it's more versatile, in the sense, we can add more conditions to the .. inputs. In particular, in our case, /^name:\s+(\S+)/ && $h{$a=$1} makes for a composite cond1 that when the line begins with name: followed by multiple whitespace and then the student name is captured and $h{$a=$1} tests whether this student name is found with a TRUE value. (N.B.: so => if ID were to be a zero this will trip up!). The flip-flop operator stays true once this step has been taken and we keep at it till we see a empty line or we hit the EOF. At this particular step we stuff the current line $_ with the id data from the hash.
4
  • Warning: Use of "eof" without parentheses is ambiguous at -e line 5. Use of ?PATTERN? without explicit operator is deprecated at -e line 5. Search pattern not terminated or ternary operator parsed as search pattern at -e line 5. thats what I've got in the terminal output. Mar 31 '17 at 17:10
  • This is perl 5, version 18, subversion 2 (v5.18.2) built for x86_64-linux-gnu-thread-multi (with 44 registered patches, see perl -V for more detail) Mar 31 '17 at 17:18
  • Thanks a lot!! it works perfectly As @StephenRauch said i would like to understand the code. I will search for it. Mar 31 '17 at 17:30
  • 2
    @CesarVillegas: Added an explanatory note to the Answer.
    – user218374
    Mar 31 '17 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.