LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-12-2012, 02:24 PM   #1
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 103

Rep: Reputation: 1
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.
 
Old 10-12-2012, 11:35 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
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
 
1 members found this post helpful.
Old 10-14-2012, 08:10 PM   #3
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 103

Original Poster
Rep: Reputation: 1
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?
 
Old 10-14-2012, 10:53 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
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
 
Old 10-15-2012, 01:46 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
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.
 
Old 10-16-2012, 07:33 AM   #6
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 103

Original Poster
Rep: Reputation: 1
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
 
Old 10-16-2012, 10:07 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] passing one variable to another coolsreejith Linux - Newbie 2 01-08-2011 10:22 PM
[SOLVED] Problem with BASH conditionals - passing condition as variable smaddox Programming 8 09-18-2009 11:21 AM
function showing a list of variable and value: (dynamic variable name) budhax Linux - Newbie 1 09-19-2008 07:05 PM
C++ passing variable argument list to other functions R00ts Programming 8 08-08-2008 05:10 PM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 02:18 PM


All times are GMT -5. The time now is 07:04 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration