Help with cut and tail

Hey!!

I'm having a hard time getting this to work!

I need to input a name and compare that name to a file in the file the name has a code on the same line as it, what i need is to compare the input name with the file and then output the code to a other file.

Ex:
code;name;xxxx;xxxx


can any one help me ??? i'm programing shell script bash
filename="something.txt"
awk -F ';'  -v fname=$filename ' $2==fname {print $1}' oldfile > newfile

Thanks but I can't get that to work

If not works, please show your input files, and desired output.

this is what I need
I type in a name that is already in a file like

code;name;location;date
code;name;location;date

now I need to find the name I inputed in the file so I can get its code
and output it's code to a other file

Maybe this then ?

$
$ cat -n f5
     1  code1;name1;location1;date1
     2  code2;name2;location2;date2
$
$ awk -F';' -v name="name1" '$2==name {print $1}' f5
code1
$
$ awk -F';' -v name="name2" '$2==name {print $1}' f5
code2
$
$

tyler_durden

can anybody tell me why is code does not work ??

it never enter's the if even when the name is right

echo "Name:"
read Name

prev=`cut -d ";" -f1-3 funcionarios.cvs`

for linha in "$prev"
do
     fun=`echo "$linha" | cut -d ";" -f2`
     if [ "$Nome" = "fun" ]; then
          echo "It's the same"
     fi
done

thanks

What is Nome in your if-statement?

If that should be Name, you should perhaps proof-read your code before asking.

Sorry it's $Name

---------- Post updated at 03:06 PM ---------- Previous update was at 03:05 PM ----------

But that's not the problem

fun is a variable, so...

if [ "$Name" = "$fun" ];

Also try using set -x in your script to debug it.

yes
ok

if [ "$Name" = "$fun" ]; then

But the rest of the code does not do what you expect it to do. You can see what happens by using echo statements with the variable names.
The preferred method for this type of operation is something like this:

echo "Name:"
read $input_name
while IFS=";" read code name location
do 
  if [ "$input_name" = "$name" ]
  then
    echo $code
  fi
done < funcionarios.csv

It does but what I don't get is why $Name and $fun have the same value but the if does not work

the file funcionarios.csv has this

001op;Jose Antonio Borges;Rua de Cima, 7, Lisboa
002op;Silvia Maria;xxxx xxxx, 8, Lisboa
003op;XXXXX ZZZZ;aaaaaa ddd, 9, Lisboa

Because "$Name" != "Jose Antonio Borges Silvia Maria XXXXX ZZZZ"

But the $Name is inputed by me!! The code is for it to look for the name in the file funcionarios.csv and if it's there in output's the code

---------- Post updated at 05:15 PM ---------- Previous update was at 05:15 PM ----------

This must be easy I must be complicating things

I guess instead of looking into Scrutinizer's suggested script, you want an explanation of why your script is not working.

I'll start out with your script and csv file that has only the first two records. I've added a couple of "echo" statements in your script to aid in debugging. They are shown in red below.

$
$ cat -n processnames.sh
     1  echo "Name:"
     2  read Name
     3
     4  echo "Value of Name : ==>$Name<=="
     5  prev=`cut -d ";" -f1-3 funcionarios.csv`
     6  echo "Value of prev : ==>$prev<=="
     7
     8  for linha in "$prev"
     9  do
    10    echo "Value of linha : ==>$linha<=="
    11       fun=`echo "$linha" | cut -d ";" -f2`
    12    echo "Value of fun : ==>$fun<=="
    13       if [ "$Name" = "$fun" ]; then
    14            echo "It's the same"
    15       fi
    16  done
$
$

The csv file looks like this -

$
$ cat -n funcionarios.csv
     1  001op;Jose Antonio Borges;Rua de Cima, 7, Lisboa
     2  002op;Silvia Maria;xxxx xxxx, 8, Lisboa
$
$

Now run the script. My shell is Bash, btw.

$
$ . processnames.sh
Name:
Jose Antonio Borges
Value of Name : ==>Jose Antonio Borges<==
Value of prev : ==>001op;Jose Antonio Borges;Rua de Cima, 7, Lisboa
002op;Silvia Maria;xxxx xxxx, 8, Lisboa<==
Value of linha : ==>001op;Jose Antonio Borges;Rua de Cima, 7, Lisboa
002op;Silvia Maria;xxxx xxxx, 8, Lisboa<==
Value of fun : ==>Jose Antonio Borges
Silvia Maria<==
$
$

Here's what happened.
(i) I entered the name "Jose Antonio Borges".
(ii) It printed the value of name "Jose Antonio Borges" at line 4.
(iii) It fetches value of prev at line 5.
(iv) It prints the value of prev at line 6. Note this value of prev:

Value of prev : ==>001op;Jose Antonio Borges;Rua de Cima, 7, Lisboa
002op;Silvia Maria;xxxx xxxx, 8, Lisboa<==

Is this what you wanted ??
Or did you want the value "001op;Jose Antonio Borges;Rua de Cima, 7, Lisboa" while processing line 1.
And "002op;Silvia Maria;xxxx xxxx, 8, Lisboa" while processing line 2.

If that's the case, then you don't have to do that. It's an unnecessary and convoluted way of doing things. You don't have to split the line twice, you can read the line and split it into tokens while reading itself. Which is what Scrutinizer is trying to tell you.

(v) The value of "linha" is the same as "prev".
(vi) And because "linha" has been botched up, the value of "fun" is incorrect too.

You probably wanted the value of "fun" to be:

  • "Jose Antonio Borges" while processing line 1.
  • "Silvia Maria" while processing line 2.

Have a look at Scrutinizer's script. It reads a line, breaks it up into appropriate tokens (while reading itself) and then compares the 2nd token with the name that you input.

Hope that clears up your doubts.

tyler_durden

Thank's i got it now