Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[solved]*nix Shell Programming
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
swingkyd
Guru
Guru


Joined: 13 Jan 2005
Posts: 334

PostPosted: Mon May 05, 2008 7:24 pm    Post subject: [solved]*nix Shell Programming Reply with quote

Hello,
I have a general question about programming using the *nix shell. For example, I have a tonne of CSV files which have lots of data I need to capture. The files are very predictable though:
The last 40 lines of each file, but append the filename to every line as the first field.
Can anyone help me solve this? If not, can anyone help me find a good tutorial where I can learn how to do this kind of stuff? Thanks a tonne!
Here is a sample file:
Quote:




DATE OF RUN (Start)= DAY 5 / Month 5 / Year 2008
STARTING TIME= 12: 7: 5:53






RUNID : MZ_001_01.0_100 Frequency Case # 1: 0.0010000 Hz



SCALAR POTENTIALS/TOUCH VOLTAGES/USER DEFINED

|--Profile---|----- Coordinates (meters)------| Touch Voltage (Volts)
Pr.No Point X Y Z Magnitude Ang.(deg) Real Part Imag. Part
====== ====== ====== ======= ======= ======= ======= ======= =======
1, 1, 30.0000000, 0.0000000, 0.0000000, 1.3910098, 0.0000000, 1.3910098, 0.0000000
1, 2, 31.2049999, 0.0000000, 0.0000000, 2.5579591, 0.0000000, 2.5579591, 0.0000000
1, 3, 32.4099998, 0.0000000, 0.0000000, 3.4811647, 0.0000000, 3.4811647, 0.0000000

And I would like the output to be streamed to a second file.
Now, I can get all the data without the filename using something like this:
Code:
#!/bin/bash
sOutFile="./out.csv"
sList=$(ls ../out/MZ_0.001_001_02.0_*.CSV)
:>$sOutFile
echo $sList
for files in $sList; do
#  echo $files>> $sOutFile
  echo $files
  tail -n 40 $files >>$sOutFile 
done

But from here, I don't know how to add the name of the file to each of the 40 lines extracted.
Thanks if you can help


Last edited by swingkyd on Tue May 06, 2008 10:40 pm; edited 1 time in total
Back to top
View user's profile Send private message
spOOwn
Apprentice
Apprentice


Joined: 02 Nov 2002
Posts: 259
Location: Belgium

PostPosted: Mon May 05, 2008 8:10 pm    Post subject: Reply with quote

here is something that could perhaps help you, you have to use awk to process each line of your for loop, look at this :
Code:

#!/bin/bash
sOutFile="./out.csv"
sList=$(ls ../out/MZ_0.001_001_02.0_*.CSV)
:>$sOutFile
echo $sList
for files in $sList; do
#  echo $files>> $sOutFile
  echo $files
  tail -n 40 $files [b] | awk -v my_var="$sList" '{print $0,my_var}'[/b] >>$sOutFile
done


I think that is what you want !
_________________
###########################
# Web site : http://sys-admin.wikidot.com #
###########################
Back to top
View user's profile Send private message
swingkyd
Guru
Guru


Joined: 13 Jan 2005
Posts: 334

PostPosted: Mon May 05, 2008 8:27 pm    Post subject: Reply with quote

with an ever-so-small modification, it does what I want. I actually wanted it to pre-pend but append is perfectly fine!
Code:
#!/bin/bash
sOutFile="./out.csv"
sList=$(ls ../out/MZ_0.001_001_02.0_*.CSV)
:>$sOutFile
echo $sList
for files in $sList; do
#  echo $files>> $sOutFile
  echo $files
  tail -n 40 $files | awk -v my_var="$files" '{print $0,my_var}' >>$sOutFile   
done

Brilliant! and thanks for saving me from pulling my hair out some more.
All I did was use the $files variable instead of $slist.
Back to top
View user's profile Send private message
swingkyd
Guru
Guru


Joined: 13 Jan 2005
Posts: 334

PostPosted: Mon May 05, 2008 8:30 pm    Post subject: Reply with quote

so, just a question, do you know of a good tutorial that can get me proficient at sed and awk? after much "googling" I found lots of examples, man pages etc. but nothing for a newbie like me. I'm wondering if people here have some links handy for help.
Back to top
View user's profile Send private message
octanez
Tux's lil' helper
Tux's lil' helper


Joined: 18 Apr 2004
Posts: 149
Location: Woods Hole, MA, USA, Earth

PostPosted: Mon May 05, 2008 8:35 pm    Post subject: Reply with quote

O'Reilly's Sed & Awk is very good. I haven't seen The Pocket Reference, they are both in the Safari colleciton too if you have a library that subscribes.
_________________
Adopt an orphan
Back to top
View user's profile Send private message
spOOwn
Apprentice
Apprentice


Joined: 02 Nov 2002
Posts: 259
Location: Belgium

PostPosted: Tue May 06, 2008 11:19 am    Post subject: Reply with quote

if you wanted to pre-pend the output and not append it, change this line :
Code:

tail -n 40 $files | awk -v my_var="$sList" '{print $0,my_var}' >>$sOutFile

by this one :
Code:

tail -n 40 $files | awk -v my_var="$sList" '{print my_var,$0}' >>$sOutFile

It change the way awk print the lines you want.
_________________
###########################
# Web site : http://sys-admin.wikidot.com #
###########################
Back to top
View user's profile Send private message
swingkyd
Guru
Guru


Joined: 13 Jan 2005
Posts: 334

PostPosted: Fri May 16, 2008 2:36 pm    Post subject: Reply with quote

I just thought I'd revisit this code (I've used this already way back when I asked the question) I just thought I'd write out some sort of pseudo code just in case anyone else has no clue like myself :)

So from what I understand, awk is a line processor. We are taking the filename and storing it as a variable, which is passed to awk (using -v my_var="$files"). Then each line ($0) is captured and printed to the output with the passed variable appended or prepended using the "print" command.

Cheers and thanks for the quick responses
Back to top
View user's profile Send private message
ziggysquatch
Apprentice
Apprentice


Joined: 16 Nov 2004
Posts: 172
Location: /USA/Minnesota

PostPosted: Fri May 16, 2008 4:22 pm    Post subject: Reply with quote

swingkyd wrote:
so, just a question, do you know of a good tutorial that can get me proficient at sed and awk? after much "googling" I found lots of examples, man pages etc. but nothing for a newbie like me. I'm wondering if people here have some links handy for help.


I read the Unix Shells by Example by Ellie Quigly and it has loads of info on how to use sed and awk among others. I think its in its fifth edition now or something.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum