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.
Hello all I'm a newbie to programming and scripting but so far I been working with a friend on a bash script to automate the download/decryption/text-editing/upload of certain files.
Basically our script downloads a file from a predetermined FTP site, the file is encrypted using PGP so the script also decrypts the file and names it. The script has a lot of code that enables it to send an e-mail wanning to me if it encounters an error (File not been in the ftp site, name of file not matching or file been corrupted etc etc). Anyways its been a fun project so far but now i have hit a wall.
My next step in the project is to do some basic text editing on the file basically i need to delete the first and last line of the file. the file looks something like this:
Everyday i will be downloading a file that will have more or less data. but everytimne i have to delete the first and last line, i have read and tried AWK with no luck
anybody can point me in the right direction please.
Well, this suggestion isn't terribly elegant, but if you are using bash, you can use
wc -l
to find how many lines in the file, then subtract 1 to get newlinecnt and use
head, tail to get all but last line & first lines eg if t.t has 9 lines, then
head -8 t.t|tail -7
chops off 1st & last lines.
You can either specify multiple commands using multiple -e arguments, or separate commands with a ;
Code:
matthew@chubby:~/tmp$ cat testfile
one Bob a-dealin'
two Bobs a-dealin'
three Bobs a-dealin'
four Bobs a-dealin'
five Bobs a-dealin'
matthew@chubby:~/tmp$ sed -e '1 d' -e '$ d' testfile
two Bobs a-dealin'
three Bobs a-dealin'
four Bobs a-dealin'
matthew@chubby:~/tmp$ sed '1 d ; $ d' testfile
two Bobs a-dealin'
three Bobs a-dealin'
four Bobs a-dealin'
It's OK for short files, but if you have a 10 GiG file with millions of lines... not so hot.
true, but there are ways to do it in Python,eg memory map, seek(),tell(), etc even a simple for loop over the lines ...
correct me if i am wrong, but even if sed, awk, grep over 10G file, its slow too right?
true, but there are ways to do it in Python,eg memory map, seek(),tell(), etc even a simple for loop over the lines ...
correct me if i am wrong, but even if sed, awk, grep over 10G file, its slow too right?
Well they'll be slow in that they'll need to read and filter a lot of data, but they'll only keep one line in memory at a time (or a few in the case of a sed program using the N command).
However, I think the approach above with python will try slurp the whole file into memory.
Well they'll be slow in that they'll need to read and filter a lot of data, but they'll only keep one line in memory at a time (or a few in the case of a sed program using the N command).
sure. with a looping in Python, it can achieve "one line in memory at a time"
Quote:
However, I think the approach above with python will try slurp the whole file into memory.
yes, you are right. the alternative assumes the file is small enough to fit into the memory of the system which its running.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.