LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help need'd toread flat file at positions 23-30,67-89 per line using shell script (https://www.linuxquestions.org/questions/linux-newbie-8/help-needd-toread-flat-file-at-positions-23-30-67-89-per-line-using-shell-script-699173/)

SanaJay 01-22-2009 12:27 PM

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

unSpawn 01-22-2009 01:00 PM

Quote:

Originally Posted by SanaJay (Post 3417603)
read flat file say at positions 23-30,67-89....per line

Maybe sed, as in 'sed -n "starting_position","end_position"p /path/to/filename'?


Quote:

Originally Posted by SanaJay (Post 3417603)
and write to another file with the fields comma separated

Your choice of sed, awk, tr?


Quote:

Originally Posted by SanaJay (Post 3417603)
Can someone please let me know how to go about it.

Basically read some tutorials. They're easy to find and being new to something shouldn't keep you from searching & trying...

TB0ne 01-22-2009 01:02 PM

Quote:

Originally Posted by SanaJay (Post 3417603)
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.

rweaver 01-22-2009 01:53 PM

Quote:

Originally Posted by SanaJay (Post 3417603)
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).

4: while (<FILE1>) {
5: print FILE2 substr($_,22,8) . "," . substr($_,66,23) . "\n";
6: }

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.

pixellany 01-22-2009 01:54 PM

http://tldp.org Get the "Bash Guide for Beginners"

http://www.grymoire.com/Unix/ Really good tutorials and SED and AWK

Ditto the comment that you should provide a sample of the file and the desired output.


All times are GMT -5. The time now is 07:52 PM.