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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-16-2005, 03:10 AM
|
#1
|
LQ Newbie
Registered: Nov 2004
Location: Milan (Italy)
Distribution: RED HAT WS 3
Posts: 2
Rep:
|
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
|
|
|
06-16-2005, 07:54 AM
|
#2
|
Member
Registered: Jan 2005
Distribution: SUSE, LFS
Posts: 357
Rep:
|
Pipe "find" into "sed"?
|
|
|
06-16-2005, 02:58 PM
|
#3
|
Member
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929
Rep:
|
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
|
|
|
06-17-2005, 03:54 AM
|
#4
|
Member
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273
Rep:
|
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
|
|
|
06-17-2005, 09:26 AM
|
#5
|
Member
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929
Rep:
|
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.
|
|
|
06-17-2005, 10:05 AM
|
#6
|
Member
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293
Rep:
|
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\" \"{}\"")}' '{}' \;
|
|
|
06-17-2005, 10:13 AM
|
#7
|
Member
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929
Rep:
|
O_o
...and I was excited when I found out about {print $1}...
*goes to find awk tutorial*
|
|
|
06-17-2005, 10:19 AM
|
#8
|
Member
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273
Rep:
|
Wow....
I'm impressed and horrified, all at the same time. You just made my day ;-)
Groetjes,
Kees-Jan
|
|
|
All times are GMT -5. The time now is 09:18 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|