ProgrammingThis 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.
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.
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 ?
$ 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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.