LinuxQuestions.org
Visit Jeremy's Blog.
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 06-16-2005, 03:10 AM   #1
lgualteri
LQ Newbie
 
Registered: Nov 2004
Location: Milan (Italy)
Distribution: RED HAT WS 3
Posts: 2

Rep: Reputation: 0
script request


All,

I have a simple question. We have a bunch of files containing only alphanumeric characters where we need to attach a header.
Note that the second parameter in the header is the first parameter of each row.

Example - the original file is:

antl 203040.000 102039.000 white 2.00 1.00
antl 203041.000 192030.000 white 2.00 1.00
antl 203043.000 102830.000 blue 2.00 1.00
antl 253040.000 102730.000 blue 2.00 1.00
antl 203640.000 102037.000 blue 2.00 1.00


The edited file shall be like:

PROFILE antl TYPE 2
antl 203040.000 102039.000 white 2.00 1.00
antl 203041.000 192030.000 white 2.00 1.00
antl 203043.000 102830.000 blue 2.00 1.00
antl 253040.000 102730.000 blue 2.00 1.00
antl 203640.000 102037.000 blue 2.00 1.00


Is there a simple way to write a script and let it recursively run over a number of files ?


many thanks
Luca
 
Old 06-16-2005, 07:54 AM   #2
Kdr Kane
Member
 
Registered: Jan 2005
Distribution: SUSE, LFS
Posts: 357

Rep: Reputation: 30
Pipe "find" into "sed"?
 
Old 06-16-2005, 02:58 PM   #3
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Before:

$ cat tempfile
antl 203040.000 102039.000 white 2.00 1.00
antl 203041.000 192030.000 white 2.00 1.00
antl 203043.000 102830.000 blue 2.00 1.00
antl 253040.000 102730.000 blue 2.00 1.00
antl 203640.000 102037.000 blue 2.00 1.00

Code:
for i in `ls`; do header=`head -n 1 "$i" | awk '{print $1}'`; header="PROFILE $header TYPE 2"; echo $header | cat - "$i" >"$i".new; done; for j in `ls`; do if [ "${j##*.}" == "new" ]; then mv $j ${j%.new}; fi; done


After:
$ cat tempfile
PROFILE antl TYPE 2
antl 203040.000 102039.000 white 2.00 1.00
antl 203041.000 192030.000 white 2.00 1.00
antl 203043.000 102830.000 blue 2.00 1.00
antl 253040.000 102730.000 blue 2.00 1.00
antl 203640.000 102037.000 blue 2.00 1.00

If you want it to actually recurse through directories, not just get every file in the current directory, it's a bit harder...I'm fairly sure the first part would be ok with an ls -R instead of just ls (and maybe a grep -v or something to make it ignore directories), but I'm not don't think that the 2nd part would. If you need that, let me know and I'll pound on it a bit more.

HTH
 
Old 06-17-2005, 03:54 AM   #4
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
Pfew... I learned some new bash syntax today. May I nevertheless propose to simplify that to:

Code:
for i in `find . -type f`; do header=`head -n 1 "$i" | awk '{print $1}'`; header="PROFILE $header TYPE 2"; echo $header | cat - "$i" >"$i".new; mv $i.new $i ; done
With recursion added as a bonus.

Groetjes,

Kees-Jan
 
Old 06-17-2005, 09:26 AM   #5
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Oh very nice I can't believe I didn't think of the 'mv $i.new $i' ....d'oh! Sometimes I think too much about things...
I'm pretty weak with find (I can manage -name and -regex, but that's about it), but that -file is definitely a handy thing to remember.

Last edited by rose_bud4201; 06-17-2005 at 12:09 PM.
 
Old 06-17-2005, 10:05 AM   #6
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Find can do a bit more, the -exec command will remove the need for the "for-in-do-done" loop.

Awk can do more too, removing the need for pipes and thus speeding things up a bit.

Code:
find . -type f -exec awk 'NR<2{print "PROFILE "$1" TYPE 2" > "{}.new"} {print >> "{}.new"} END{system("mv  \"{}.new\" \"{}\"")}' '{}' \;
 
Old 06-17-2005, 10:13 AM   #7
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
O_o

...and I was excited when I found out about {print $1}...

*goes to find awk tutorial*
 
Old 06-17-2005, 10:19 AM   #8
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
Wow....
I'm impressed and horrified, all at the same time. You just made my day ;-)

Groetjes,

Kees-Jan
 
  


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
Request for a simple net script edit <Ol>Origy Linux - Networking 0 06-03-2005 12:51 PM
request from every one the anti-riced General 3 04-21-2004 12:37 PM
greetings and request portable oxygen Linux - Software 6 10-07-2003 07:21 PM
Challanging Script Request dolvmin Linux - Software 2 09-17-2003 10:26 AM
strange request: script to track logged in time hamster Linux - General 3 05-08-2003 12:52 PM

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

All times are GMT -5. The time now is 12:22 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