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 want to remove the first line of a CSV using AWK.
I could do this in PHP but then what i'd have to do is read in the CSV, unset the first element of the array and fwrite the data back. but the CSV is HUGE and i don't want to take up all that memory.
so, how would i use awk to remove the first line of a CSV? i suppose we would remove characters till we come accross a CR or LF but how would i do that?
also, i'll have to run this as a cron. any guidance?
NR = Number of Record (Row?... not sure...) which is, by default, seperated on a newline. so if the value of NR is 1 then it is on the first line, so ignore it. otherwise print the whole line.
ideally, it would read only the first line and delete it along with the CR/LF character at the end so that the rest of the records move up by a row.
can i do that in awk?
you should never write directly back to the same file. if you want to automate this, write to say, filename.tmp and then rename the file to overwrite the original one once it's completed.
so then awk accesses filename.csv, reads all its contents to memory, ignores the first line and prints everything else to stdout?
i don't think this is what happens. the file is not read into memory. you can work on files that are larger than your available ram.
also, being able to replace the file by redirecting the stdout stream may depend on the shell, so i can't say this for all command lines. but if you try this in bash, you will get an empty file. when you redirect the stdout to a file, like '$ command > file.txt', the first thing the shell does is create the file 'file.txt'. so by the time gawk tries to read it, it is an empty file.
if you type the command '$ ls > newfile.txt', you will see the file 'newfile.txt' listed in itself.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.