LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2004, 10:32 AM   #1
kscott121
Member
 
Registered: Jul 2003
Location: NC
Distribution: Fedora,Mepis,Debian
Posts: 84

Rep: Reputation: 15
howto add filename to each record?


I have a series of similar text files of similar format. I would like to concatenate the filename to every line of each file. Then as a second step concatenate all the files together so I can read them into a spreadsheet (OpenOffice of course).

Example filename abcd1.dat
contents:
textfield1, textfield2, textfield3
textfield4, textfield5, textfield6
...

desired output contents:
filename, textfield1, textfield2, textfield3
filename, textfield4, textfield5, textfield6
...

I figure this probably a simple 2 line script but I have not yet figured out sed, awk etc.

Thanks in advance.
Ken
 
Old 09-13-2004, 11:03 AM   #2
aiza
LQ Newbie
 
Registered: Jul 2004
Posts: 18

Rep: Reputation: 0
Re: howto add filename to each record?

Quote:
Originally posted by kscott121
I have a series of similar text files of similar format. I would like to concatenate the filename to every line of each file. Then as a second step concatenate all the files together so I can read them into a spreadsheet (OpenOffice of course).

Example filename abcd1.dat
contents:
textfield1, textfield2, textfield3
textfield4, textfield5, textfield6
...

desired output contents:
filename, textfield1, textfield2, textfield3
filename, textfield4, textfield5, textfield6
...

I figure this probably a simple 2 line script but I have not yet figured out sed, awk etc.

Thanks in advance.
Ken
for file in `ls *.dat` ; do
for line in `cat $file` ; do
echo $file, $line >> $file.new
done
done
 
Old 09-13-2004, 04:42 PM   #3
kscott121
Member
 
Registered: Jul 2003
Location: NC
Distribution: Fedora,Mepis,Debian
Posts: 84

Original Poster
Rep: Reputation: 15
Thanks ,
That helped a lot. It does do an odd thing though. It separates each of the comma separated tokens (textfield1, etc) from the original file into it's own line in the new file with the filename in fron of each (rather than the desired behavior of keeping all the tokens together in single line with the filenam at the start).
IE
input line from file abcd1.dat
aaaaa,bbbb,ccc

right now produces
abcd1.dat,aaaaa
abcd1.dat,bbbb
abcd1.dat,ccc

rather than the desired
abcd1.dat,aaaaa,bbbb,ccc

Any idea how to correct this?
Thanks.
Ken
 
Old 09-13-2004, 11:53 PM   #4
aiza
LQ Newbie
 
Registered: Jul 2004
Posts: 18

Rep: Reputation: 0
Quote:
Originally posted by kscott121
Thanks ,
That helped a lot. It does do an odd thing though. It separates each of the comma separated tokens (textfield1, etc) from the original file into it's own line in the new file with the filename in fron of each (rather than the desired behavior of keeping all the tokens together in single line with the filenam at the start).
IE
input line from file abcd1.dat
aaaaa,bbbb,ccc

right now produces
abcd1.dat,aaaaa
abcd1.dat,bbbb
abcd1.dat,ccc

rather than the desired
abcd1.dat,aaaaa,bbbb,ccc

Any idea how to correct this?
Thanks.
Ken
Code:
for file in `ls *.dat` ; do
    exec < $file
    while read line
    do
        echo $file, $line
    done
done
 
Old 09-14-2004, 10:13 AM   #5
kscott121
Member
 
Registered: Jul 2003
Location: NC
Distribution: Fedora,Mepis,Debian
Posts: 84

Original Poster
Rep: Reputation: 15
Thanks!
That worked excellently.
I also now can possibly learn something from your fine example.!!

Ken
 
  


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
How do I add the current date to a filename? sammysrefuge Linux - Newbie 2 03-18-2005 02:43 PM
Howto setup MX record Airwaves Linux - Networking 5 12-13-2004 05:35 AM
MX record howto setup kafnir Linux - Networking 3 06-16-2004 08:58 AM
Script add date to filename amphion Linux - Newbie 2 06-02-2004 07:12 AM
how to add date to filename x2000koh Programming 1 07-08-2003 09:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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