LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-05-2009, 05:32 PM   #1
baidym
LQ Newbie
 
Registered: Oct 2007
Posts: 16

Rep: Reputation: 0
variable strings in a script


Hi all,

I have a set of numbers under some headers in a file:

FNUM,BNUM,C1N,C2N,C3N,C1S,C2S,C3S,
4535, 109, 5.5955, 5.6523, 5.8084, 311.419, 295.2540, 286.9984,
4536, 109, 5.7667, 5.7174, 5.6771, 291.782, 285.0646, 271.5945,
4537, 109, 5.1433, 5.3641, 5.4985, 301.515, 312.0447, 293.8658,

I have a script to take columns 1, 3, 4, 5..8 multiply by 100 and output the result in a file.

Quote:
set file = $file.csv
# Use awk to correct the format and multiply by 100
awk '{if(NR!=1) printf("%-5d%-5d%-5d\t%-5d%-5d%-5d\n",$1,($3*100),($4*100),($5*100),($6*100),($7*100),($8*100))}' $file > newfile
Which will give me something like this (the numbers here aren't correct)
FNUM C1N C2N C3N C1S C2S C3S,
4535 1982 2002 1894 10918 9680 8698
4536 1865 1885 1883 11082 9592 8682
4537 1984 1972 9620 10940 8631 7653

I would like to have the script do this regardless of how many columns are in the file so if I had a file with 4CN and 4CS it would do the same thing to the extra columns.

FNUM,BNUM,C1N,C2N,C3N,C4N,C1S,C2S,C3S,C4S
4535, 109, 5.5955, 5.6523, 5.8084, 5.2584, 291.419, 295.2540, 286.9984, 294.6548,
4536, 109, 5.7667, 5.7174, 5.6771, 5.4984, 291.782, 285.0646, 271.5945, 284.5648,

I.e. it would ask for the number of columns in the file and output 9 columns (again these are just random numbers as an example):
FNUM C1N C2N C3N C4N C1S C2S C3S C4N
4535 1982 2002 1894 1918 9680 8698 1968 4984
4536 1865 1885 1883 1082 9592 8682 6959 5593

does anyone show me how to do this? (in shell or in perl)

Many thanks,

Michelle.
 
Old 09-05-2009, 07:34 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Not sure what you're looking for, or what shell you prefer programming in (I'm guessing "bash", which is feature-rich), but here's a simple example:
Quote:
vi myfile.txt ->
chocolate strawberry vanilla
raspberry rocky_road chocolate_chip
Quote:
Code:
for i in `cat myfile.txt`;do
  echo "My favorite flavor is $i..."
done
My favorite flavor is chocolate...
My favorite flavor is strawberry...
My favorite flavor is vanilla...
My favorite flavor is raspberry...
My favorite flavor is rocky_road...
My favorite flavor is chocolate_chip...
'Hope that helps .. PSM
 
Old 09-06-2009, 05:29 AM   #3
baidym
LQ Newbie
 
Registered: Oct 2007
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks for the reply,

What I'd like to do is have the script (in this case c shell, but perl would be just as good) ask "how many values of C?", or "how many columns?" so that if I have 7 columns in one file (FNUM, 3 CNs and 3 CSs) and 9 in another (FNUM, 4 CNs and 4 CSs) I wont have to edit the script every time.

for example
Quote:
echo "how many columns?"
set col = $<

foreach $col
awk '{if(NR!=1) printf("%-5d\n",($col*100))}' $file > newfile
end
Anyone have any ideas?
M
 
  


Reply



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
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM
a script to match 2 ip strings nass Slackware 2 05-30-2007 03:49 PM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM
script to exclude strings... NiallC Programming 10 03-28-2005 06:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration