LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-22-2009, 01:27 PM   #1
SanaJay
LQ Newbie
 
Registered: Jan 2009
Posts: 6

Rep: Reputation: 0
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
 
Old 01-22-2009, 02:00 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
Quote:
Originally Posted by SanaJay View Post
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 View Post
and write to another file with the fields comma separated
Your choice of sed, awk, tr?


Quote:
Originally Posted by SanaJay View Post
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...
 
Old 01-22-2009, 02:02 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,333

Rep: Reputation: 3891Reputation: 3891Reputation: 3891Reputation: 3891Reputation: 3891Reputation: 3891Reputation: 3891Reputation: 3891Reputation: 3891Reputation: 3891Reputation: 3891
Quote:
Originally Posted by SanaJay View Post
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.
 
Old 01-22-2009, 02:53 PM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by SanaJay View Post
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.

Last edited by rweaver; 01-22-2009 at 03:07 PM. Reason: link to doc, explaination
 
Old 01-22-2009, 02:54 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 07:07 PM
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 08:32 AM
shell script read line from file, use it in command DiGiGoth Programming 5 08-31-2008 12:08 PM
shell script: insert line in a file noir911 Programming 6 02-04-2008 11:42 PM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 02:39 PM


All times are GMT -5. The time now is 05:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration