Help need'd toread flat file at positions 23-30,67-89 per line using shell script
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Help need'd toread flat file at positions 23-30,67-89 per line using shell script
Hi,
I am a newbie to scripting.I need to read flat file say at positions 23-30,67-89....per line and write to another file with the fields comma separated in each line.
Can someone please let me know how to go about it.Also it would be helpful if the steps can be explained.
Thanks in advance,
Sana
Hi,
I am a newbie to scripting.I need to read flat file say at positions 23-30,67-89....per line and write to another file with the fields comma separated in each line.
Can someone please let me know how to go about it.Also it would be helpful if the steps can be explained.
Thanks in advance,
Sana
What kind of script? Bash? Perl? Ruby? Python?? You can do what you need in any of them. From a bash script, you can use awk and sed to do this.
There are many bash scripting tutorials out on the internet, and Google can help you find them. You're asking a vague question, with no samples of data, so it's hard to say step-by-step what you need to do, other than being very vague as well.
Hi,
I am a newbie to scripting.I need to read flat file say at positions 23-30,67-89....per line and write to another file with the fields comma separated in each line.
Can someone please let me know how to go about it.Also it would be helpful if the steps can be explained.
Thanks in advance,
Sana
It would be fairly trivial to do in perl ...
Code:
#!/usr/bin/perl
open(FILE1,"<./file.in") or die "Error: $!";
open(FILE2,">./outfile.csv") or die "Error: $!";
while (<FILE1>) {
print FILE2 substr($_,22,8) . "," . substr($_,66,23) . "\n";
}
close(FILE1);
close(FILE2);
exit();
This is off top of my head and may or may not actually work for your stuff but you were pretty vague, it should give you the results you're looking for as described above if your data is relatively simple and shouldn't be hard to modify.
The help page for substring on perldoc.perl.org should help you if you're confused about how to use substr.
Oh yah, explaination:
1: #!/usr/bin/perl
It's a perl script.
2: open(FILE1,"<./file.in") or die "Error: $!";
3: open(FILE2,">./outfile.csv") or die "Error: $!";
Open one file for input (datafile), one for output (csv file).
print the substr of the line starting from char 22 including next 8 chars add a comma do the same for char 66 including next 23 chars and add a newline.
Repeat while there are lines in the input file
7: close(FILE1);
8: close(FILE2);
9: exit();
Close file 1 and 2, take your toys, go home.
Last edited by rweaver; 01-22-2009 at 02:07 PM.
Reason: link to doc, explaination
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.