LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem passing variable in a list (https://www.linuxquestions.org/questions/linux-newbie-8/problem-passing-variable-in-a-list-4175431904/)

smturner1 10-12-2012 02:24 PM

Problem passing variable in a list
 
What I want to do is grab all invoice files in the pwd and cut two of the fields. One of the field designates the manufacturer which I will need to create the unique file. The second field I will copy to the file that is uniquely named by its manufacturer, call it a car.

First call the files in the pwd.
Code:

for i in `cat *inv`
Read the fields.
Code:

do
echo $i | cut -d"|" -f2 | read x
echo $i | cut -d"|" -f4 | read y

Conditional statement based upon the input in $x. Copy field 4 from var $Y to unique file based on list.
Code:

if [$x = 1='GM', 2='Ford', 3='Dodge']
then
    echo $y && >> $x

This maybe a mess and is way far from where it needs to be, but I am willing to do whatever it takes to get it where it needs to be.

Any help would be much appreciated.

grail 10-12-2012 11:35 PM

Firstly, using for is not a good way to read a file. It is better to use while as you will then not get incorrect data based on word splitting.

Code:

#awk
awk '{print $2 > $4}' *inv

#bash
for file in *inv
do
    while read -r _ f2 _ f4 _
    do
        echo "$f2" > "$f4"
    done<file
done

Change > for >> if you need to append and not overwrite a file

smturner1 10-14-2012 08:10 PM

Thank you grail.

I am very aware of the fact that I need to learn awk, any threads or literature that you can suggest so that I can become better at it?

grail 10-14-2012 10:53 PM

Just the main manual site:

http://www.gnu.org/software/gawk/man...ode/index.html

It reads easily and you should more often then not be able to find a solution to your problem :)

David the H. 10-15-2012 01:46 AM

The grymoire tutorial is good too, although it's written for non-gnu versions of awk and has some typos. Here are a couple of other useful pages as well.

http://www.grymoire.com/Unix/Awk.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/


And read here for why you Don't Read Lines With For.

smturner1 10-16-2012 07:33 AM

Thank you as well 'David the H'.

My education feels like its just begun! The problem is that I was a Sys Admin (Network) in a department that ran UNIXWare 5.1 on a 4ESS and 5E switch. I did that for two years and depended completely on the scripts other people wrote. Of course, I wrote a couple here and there but never enough to be proficient at it. Now I am in a position that requires me to write scripts frequently. Basically I have been negligent in my education and am currently behind the ball, so-to-speak.

Any help is great. Forgive me if my frustration or lack of knowledge comes out - I am trying!

s

grail 10-16-2012 10:07 AM

As lonas you show effort you will be rewarded with a plethora of knowledge and feedback. I know have :)


All times are GMT -5. The time now is 12:29 PM.