Help with cut and tail


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with cut and tail
# 15  
Old 12-24-2009
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
# 16  
Old 12-24-2009
Quote:
Originally Posted by nogame11
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...
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.

Code:
$
$ 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 -

Code:
$
$ 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.

Code:
$
$ . 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:

Code:
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
# 17  
Old 12-28-2009
Thank's i got it now
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

3. Shell Programming and Scripting

Joining multiple files tail on tail

I have 250 files that have 16 columns each - all numbered as follows stat.1000, stat.1001, stat.1002, stat.1003....stat.1250. I would like to join all 250 of them together tail by tail as follows. For example stat.1000 a b c d e f stat.1001 g h i j k l So that my output... (2 Replies)
Discussion started by: kayak
2 Replies

4. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

5. Shell Programming and Scripting

tail, grep and cut

Hello all, I have some weird problem that kinda baffles me. Say I have the following test file: claudia:~/tmp$ cat testfile.txt This is a test line This is the second test line And yeah, this is the third test line Then say I want to tail the file, grep for the word "third" then... (7 Replies)
Discussion started by: sylaan
7 Replies
Login or Register to Ask a Question